我尝试将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)
我认为这是由变量或方法的错误类型(静态,公共,etv。)引起的。我的假设是正确的吗?
我找不到我的错误。
答案 0 :(得分:0)
来自logcat的这一行:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void de.aintlarry.patsch.ViewEventsActivity.setEventsToView()' on a null object referenc
表示当您对其执行setEventsToView时,viewEventsActivity为null。我没有在你的代码中看到viewEventsActivity被声明/初始化。如果您的AsyncTask嵌套在您的活动类中,那么您可以使用:ViewEventsActivity.this.setEventsToView(listString);
访问setEventToView - 但重要的是您应该从AsyncTask的onPostExecute
执行它 - 它在UI线程上执行,这是您可以在哪里更新视图。