Android: java.lang.IllegalStateException: adapter的内容已经改变但是ListView没有收到通知

Android: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification

异常提示我调用 notifyDataSetChanged(),但我已经在我的 onPostExecute 方法上调用了该方法,如您所见:

Constants.newsDownloaded = true;
TempStorage.downloadedNews = this.items;
this.progress.setVisibility(View.INVISIBLE);
this.downloadMore.setVisibility(View.VISIBLE);
adapter = new NewsListAdapter(context,
                inflater, this.items);

if (index <= 26){
    listView.setSelection(0);
} else{
    listView.setSelection(TempStorage.firstAddedIndex);
}

listView.setAdapter(adapter);
adapter.notifyDataSetChanged();

如您所见,每次调用都会重新创建适配器。我只在 Nexus 5 上遇到过这个问题,在其他设备上运行完美。

这段代码有什么问题?

谢谢大家的帮助!

我认为您应该删除 notifyDataSetChanged 行。无论如何,您正在创建一个新的适配器实例,为什么要在之后立即通知它?它具有正确的更新数据,因为您有一个新实例。

我曾经在类似的情况下遇到过同样的崩溃,更新我在适配器中查询的数据。通过在我的 onPostExecute 中创建一个新的适配器实例,我摆脱了崩溃。