我在后台创建一个ListView的内容,并且当添加每个项目时,我更新ListView适配器。通常它工作正常,但有时候我得到此错误。奇怪的是,它在我的Galaxy S4迷你版中发生的次数要比我的HTC Sensation多出10倍。我不明白为什么会发生这种情况,因为我明确通过UI线程通知适配器。有什么想法吗 ?Android - 在AsyncTask中创建ListView内容导致随机崩溃
java.lang.IllegalStateException: The content of the adapter has changed but ListView
did not receive a notification. Make sure the content of your adapter is not modified
from a background thread, but only from the UI thread. Make sure your adapter calls
notifyDataSetChanged() when its content changes. [in ListView(2131100779, class
util.TouchInterceptor) with Adapter(class com.bill.deuterh.ListActivity$ListAdapter)]
的AsyncTask:
private class AsyncCaller extends AsyncTask<Void,Void,Void>{
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
ListActivity.this.runOnUiThread(new Runnable(){
@Override
public void run() {
findViewById(R.id.progress_bar).setVisibility(View.GONE);
}
});
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
for (int i=0;i<table.length;i++){
if(isCancelled()){break;}
myList.add(createObject(table[i])));
ListActivity.this.runOnUiThread(new Runnable(){
@Override
public void run() {
ListAdapter.notifyDataSetChanged();
}
});
}
return null;
}
}
(如果它的事项,我投列表视图作为TouchInterceptor其是用于支持与拖放列表视图项的重排的改性谷歌类。)
为什么不把'Listadapter.notifyDataSetChanged()'放到'onProgressUpdate()'中并通过'publishProgress'调用更新。人们,asynctask带有UI运行方法!使用它们。 'AsyncTask'功能强大,但不是以这种方式使用它 – Mike 2014-12-13 12:30:38
在OnPostExecute中设置ListView适配器。 – 2014-12-13 12:30:43