android adapter报错The content of the adapter has changed but ListView did not receive a notification.

在Android编程中使用Adapter时,偶尔会出现如下错误:

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。

DDMS中的log也无法定位到准确的出错位置。检查错误可以从下面几点入手:

1、检查Thread,确定没有在Background thread中直接调用adapter,如果有,请移除相关代码到Handler中处理;

2、尽量将数据放在adapter类中管理,不需要的时候清除信息(勤写clear()),及时用notifyDataSetChanged()刷新;

3、在Activity或者Fragment合适的位置(onPause/onStop)要及时检查thread,有adapter数据处理相关的应马上停止;

4、这个错误经常出现在Activity休眠起来之后,主要还是使用adapter不太小心造成的。如果实在找不到原因,在onPause()函数中停止所有的background thread,并且在onResume()函数最前面清空adapter中的数据,并且adapter.notifyDataSetChanged()。然后重新更新加载数据,这样一般可以解决问题。

-----------------------------------------

很多新手在Android开发时可能对Thread和Message以及Handler、Looper概念不是很清楚,可能产生类似 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.的错误。在这里给出以下几点可以帮助你了解该问题:

  1.改变适配器Adapter内容时不要在后台线程中,必须在UI线程中处理,这点可以通过Handler传出来解决。

  2.尝试Adapter的notifyDataSetChanged()方法,当然主要看你代码的具体情况。

  以上只貌似只是把错误信息翻译过来而已,真正的原因请看源代码:

  mItemCount = mAdapter == null ? 0 : mAdapter.getCount();

  ..... //中间是其他代码就不拷贝了

  else if (mItemCount != mAdapter.getCount()) {//这里可以看出为什么会抛出adapter has changed,是因为你可能还有线程在跑,更新了mAdapter.getCount();的返回值

  throw new 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. [in ListView(" + getId() + ", " + getClass()

  + ") with Adapter(" + mAdapter.getClass() + ")]");

  ============

  ps:知其然,知其所以然。刚开始我发现写不写notifyDataSetChanged()都能实现效果,于是我就把它注释了,直到出了问题,我看了以上内容,我就明白为什么会有adapter has changed的提示,也明白了notifyDataSetChanged()的含义。

  我的理解是:增加了内容长度改变了 但是没有通知更新 当选中增加的那个内容时 就造成了 数组越界(或者别的问题)

  所以添加内容后要记得更新。

时间: 2024-08-25 18:30:16

android adapter报错The content of the adapter has changed but ListView did not receive a notification.的相关文章

android 修改listview中adapter数据时抛出异常java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification问题

近日在做项目时遇到非必现crush,具体异常信息为: // Short Msg: java.lang.IllegalStateException // Long Msg: 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 mo

Android The content of the adapter has changed but ListView did not receive a notification

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 原因:没有在主线程里通知.... 1.bug 出现的地方 listView.class 1487行 if (mIte

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

ListView UI重绘时触发layoutChildren, 此时会校验listView的mItemCount与其Adapter.getCount是否相同,不同报错. ListView.layoutChildren: mItemCount是在父类AdapterView中定义的,package类型 在两个地方mItemCount会被赋值,初始设置Adapter时: xxAdapter.notifyDataSetChanged时: 因此一定要确保修改adapter数据和notifyDataSetC

The content of the adapter has changed but ListView did not receive a notification

在非Ui线程中修改ListView绑定的数据对象的问题 问题描述: 在非UI线程中修改了ListView绑定的数据对象(如List)时,如下异常: 问题分析: 在非UI线程下不能够对主线程中ListView绑定的List数据进行修改. 解决方法: 方法一:将对List中数据的修改放到主线程中. 方法二:将子线程中要更新的数据通过Handler传递到主线程中,然后在主线程中将传递过来的数据添加到ListView绑定的数据对象List中,然后同时adapter数据改变. 在这里主要介绍第二种方式:

因为ndk路径错误的android打包报错

执行:编译cocos2d里的android项目 报错:[taskdef] could not load definitions from resource anttasks. properties. It could not be found 原因:/F:/bundle/android-ndk/android-ndk-r10d  ndk路径前面多了一个斜杠  "/" 知识点:window的路径前面没有"/"    linux和mac的才是"/"

Android 工程报错解决 Unable to resolve target 'android-17'

转自:http://www.cnblogs.com/csulennon/p/3705177.html 换了系统后,重新安装了Android SDK和ADT插件,导入之前的工作空间.居然发现所有的Android工程都报错了. 我之前的SDK版本是用的17,升级后用的是19. 1: [2014-05-03 14:56:25 - ThinkBlink] Unable to resolve target 'android-17' 2: [2014-05-03 14:56:26 - Game2048Pub

android模拟器 报错:X Error of failed request: BadRequest (invalid request code or no such operation)

最近在ubuntu12.04上学习python,python2.7 python3.2差别还是蛮大的,就想着学习较新的 升级后 结果出现 输入法不显示   update-manager 和  add-apt-repository不能用,android的模拟器也打不开. 罪魁祸首直至python 因为python3.2没有往上兼容,导致从python2.7升级到3.2 很多依赖之前2.7版本的功能无法使用 如何解决python升级的问题 请 http://book.51cto.com/art/20

Android打包报错 Export aborted because fatal lint errors were found. These are listed in the Lint View

打包时报如下错误: Export aborted because fatal lint errors were found. These are listed in the Lint View. Either fix these before running Export again,or turn off"Run full error check when exporting app" in the Android > Lint Error Checking preferenc

Android Studio报错:Failed to complete Gradle execution.

解决Android Studio报错:Failed to complete Gradle execution. 运行Android Studio的时候,出现以下错误: 原因:用于启动虚拟机的内存不够,android studio的默认最大内存是10g,所以对于4g.8g内存的电脑来说,内存不够用,不过为什么有时不会出问题有时会,可能是google优化不好吧,但这又有个问题,把参数改为1g.2g都不行,一定要更小才行,只能说明Android studio确实有bug. 解决方法 1: 解决方法 2