gpt4 book ai didi

java - 无法将 ArrayList 从 AsyncTask 传递到 Activity (Android)

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:26 27 4
gpt4 key购买 nike

我尝试将 AsyncTask 中的 ArrayList 过度放入 Activity 中。但结果是 NullPointerException。

这是我的代码

异步任务

ArrayList<String> listString = new ArrayList<String>();

private class gettingEvents extends AsyncTask<String, Void, String>{
protected String doInBackground(String... urls) {

String tableName = makeTableName();

try {

String url = "jdbc:mysql://"+server+":"+port+"/"+dbName;
connection = DriverManager.getConnection(url, username, password);
Statement statement = null;
String sqlCommand = null;

DatabaseMetaData md = (DatabaseMetaData) connection.getMetaData();
ResultSet rsDB = md.getTables(dbName,null,null,null);
while (rsDB.next()){

ArrayList<Player> players = new ArrayList<Player>();

String currTableName = rsDB.getString("TABLE_NAME");

statement = connection.createStatement();
sqlCommand = "SELECT * FROM " + currTableName;
ResultSet rsTB = statement.executeQuery(sqlCommand);
while(rsTB.next()){
players.add(new Player(rsTB.getString("name"),rsTB.getInt("money"), rsTB.getInt("tip")));
}

listString.add(makeEventString(currTableName, players));

}

connection.close();

}catch (SQLException sqle){

}

viewEventsActivity.setEventsToView(listString);

return null;
}
}

接收类:

public class ViewEventsActivity  extends AppCompatActivity {

MySQLHandler mySQLHandler = new MySQLHandler();;

static ListView lvShowEvents;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewevents);

lvShowEvents = (ListView) findViewById(R.id.lvShowEvents);

mySQLHandler.startGettingEvents();
}

public void setEventsToView(ArrayList<String> listStringArray){

ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(ViewEventsActivity.this, R.layout.list_items, listStringArray);

lvShowEvents.setAdapter(listAdapter);
lvShowEvents.setVisibility(View.VISIBLE);

}

}

这是 LogCat:

01-05 22:14:55.875 32144-32382/de.aintlarry.patsch E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: de.aintlarry.patsch, PID: 32144
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void de.aintlarry.patsch.ViewEventsActivity.setEventsToView()' on a null object reference
at de.aintlarry.patsch.MySQLHandler$gettingEvents.doInBackground(MySQLHandler.java:237)
at de.aintlarry.patsch.MySQLHandler$gettingEvents.doInBackground(MySQLHandler.java:200)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 

我认为这是由变量或方法的错误类型(静态、公共(public)等)引起的。我的假设正确吗?

我找不到我的错误。

最佳答案

logcat 中的这一行:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void de.aintlarry.patsch.ViewEventsActivity.setEventsToView()' on a null object referenc

表示当您对其执行setEventsToView时,viewEventsActivity为空。我在您的代码中没有看到 viewEventsActivity 被声明/初始化的位置。如果您的 AsyncTask 嵌套在您的 Activity 类中,那么您可以通过以下方式访问 setEventToView:ViewEventsActivity.this.setEventsToView(listString); - 但重要的是您应该从 AsyncTask 的 onPostExecute 执行此操作 - 它在 UI 线程上执行,您可以在此处更新 View 。

关于java - 无法将 ArrayList 从 AsyncTask 传递到 Activity (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34621542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com