【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
原文网址:http://blog.csdn.net/ueryueryuery/article/details/20607845
我写了一个Dialog,Dialog中有一个ListView,想要点ListView中的一项后,跳转到另外一个Activity去。
但在使用时,会偶尔报出下面的错误:
02-21 14:54:28.928: E/AndroidRuntime(2846): FATAL EXCEPTION: main
02-21 14:54:28.928: E/AndroidRuntime(2846): 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. [in ListView(2131165196, class android.widget.ListView) with Adapter(class com.jovision.multiscreen.views.DeviceScanSelectDialog$DeviceAdapter)]
02-21 14:54:28.928: E/AndroidRuntime(2846): at android.widget.ListView.layoutChildren(ListView.java:1510)
02-21 14:54:28.928: E/AndroidRuntime(2846): at android.widget.AbsListView.onTouchModeChanged(AbsListView.java:2077)
02-21 14:54:28.928: E/AndroidRuntime(2846): at android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:591)
02-21 14:54:28.928: E/AndroidRuntime(2846): at android.view.ViewRoot.ensureTouchModeLocally(ViewRoot.java:2095)
02-21 14:54:28.928: E/AndroidRuntime(2846): at android.view.ViewRoot.performTraversals(ViewRoot.java:809)
02-21 14:54:28.928: E/AndroidRuntime(2846): at android.view.ViewRoot.handleMessage(ViewRoot.java:1861)
02-21 14:54:28.928: E/AndroidRuntime(2846): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 14:54:28.928: E/AndroidRuntime(2846): at android.os.Looper.loop(Looper.java:130)
02-21 14:54:28.928: E/AndroidRuntime(2846): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-21 14:54:28.928: E/AndroidRuntime(2846): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 14:54:28.928: E/AndroidRuntime(2846): at java.lang.reflect.Method.invoke(Method.java:507)
02-21 14:54:28.928: E/AndroidRuntime(2846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
02-21 14:54:28.928: E/AndroidRuntime(2846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
02-21 14:54:28.928: E/AndroidRuntime(2846): at dalvik.system.NativeStart.main(Native Method)
其中错误描述:
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.
的意思大体是,你的adapter的内容变化了,但是你的ListView并不知情。请保证你adapter的数据在主线程中进行更改!
知道了原因,改起来就好办多了,我将我的adapter类改为:
- private class DeviceAdapter extends BaseAdapter {
- private LayoutInflater inflater;
- private ArrayList<Device> devices;
- public DeviceAdapter() {
- inflater = LayoutInflater.from(mContext);
- }
- @SuppressWarnings("unchecked")
- public void setDeviceList(ArrayList<Device> list) {
- if (list != null) {
- devices = (ArrayList<Device>) list.clone();
- notifyDataSetChanged();
- }
- }
- public void clearDeviceList() {
- if (devices != null) {
- devices.clear();
- }
- notifyDataSetChanged();
- }
- @Override
- public int getCount() {
- return devices == null ? 0 : devices.size();
- }
- 以下略)
相对于原来,我做了两项改动:
1.将所有数据“完全”保存在adapter内部,即使有外部数据进入,也会用.clone()重新生成副本,保证了数据完全是由adapter维护的。
2.保证所有setDeviceList()/clearDeviceList()是从主线程里调用的,如何保证是从主线程中调用的呢:
a.调用Activity.runOnUIThread()方法;
b.使用Handler(其实这并不非常准确,因为Handler也可以运行在非UI线程);
c.使用AsyncTask。
希望能帮到遇到同样问题的同学~
【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题的更多相关文章
- 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.layout ...
- ListView:The content of the adapter has changed but ListView did not receive a notification终极解决方法
使用ListView时遇到如下的异常信息: 10-26 18:30:45.085: E/AndroidRuntime(7323): java.lang.IllegalStateException: T ...
- Android The content of the adapter has changed but ListView did not receive a notification终极解决方法
这几天做一个自动扫描SD卡上所有APK文件的小工具,扫描过程中会把APK添加到LISTVIEW中显示,结果出现以下错误:(有时候触摸更新数据时候,触摸listview也会报错) E/AndroidRu ...
- Android开发-- The content of the adapter has changed but ListView did not receive a notification - With AsyncTask
最近在联系开发DaysMatter时遇到一个问题: app中使用ListView来展示所有事件,每次添加完事件后使用下面代码来更新ListView. toDoListView.refreshDrawa ...
- The content of the adapter has changed but ListView did not receive a notification
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive ...
- 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 co ...
- 解决java.lang.IllegalStateException: The application’s PagerAdapter changed the adapter’s content
A界面中有viewpager的动态加载,从界面A跳到界面B,再finish掉B返回A时报出此异常. java.lang.IllegalStateException: The application's ...
- 解决java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext问题
使用ClassPathXmlApplicationContext加载项目时, ClassPathXmlApplicationContext context = new ClassPathXmlAppl ...
- 解决java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext这个问题
今天在运行别人的SSH项目时,遇到了这个问题 严重: Exception sending context initialized event to listener instance of class ...
随机推荐
- if(!!attr)是什么鬼???
看到很多代码if(!!attr),为什么不直接写if(attr):其实这是一种更严谨的写法:请测试 typeof 5和typeof !!5的区别.!!的作用是把一个其他类型的变量转成的bool类型.
- python socket实例练习
Web Server是基于Socket编程,又称之为网络编程,socket是网络编程接口,socket可以建立网络连接,读数据,写数据.socket模块定义了一些常量参数,用来指定socket的的地址 ...
- sql列转行
1.需要实现一个单行的统计报表 思路先用一个union查出单列,然后再把单列转成单行 2.实现 SELECT MAX(CASE WHEN type = 1 THEN num ELSE 0 END) A ...
- 如何使用easyUI
一.简介 以下内容来自百度: jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的 目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面.开发者不 ...
- bash:command not found
在linux下执行某一常用命令时,提示类似错误信息:bash:bash:command not found 原因是所执行的命令在当前系统环境变量里找不到路径. 例如:刚安装了openOffice时,执 ...
- (一)《Java编程思想》学习——按位运算符、移位运算符
(第三章) (一)按位运算符 按位逻辑运算符有: “与”(AND) & 1&1=1;1&0=0;0&0=0 “或”(OR) | 1|1=1;1|0=1;0 ...
- java语句与流程控制
java程序结构按照结构化程序的思想分为顺序结构,选择结构,和循环结构. ①选择语句 选择结构分为单选择,双选择和多选择.双选择是标准的选择结构,单选择是双选择的简化形式,多选择是双选择的嵌套形式. ...
- C复习手记(Day3)
C预处理器 C 预处理器不是编译器的组成部分,但是它是编译过程中一个单独的步骤.简言之,C 预处理器只不过是一个文本替换工具而已,它们会指示编译器在实际编译之前完成所需的预处理.我们将把 C 预处理器 ...
- Oracle怎样方便地查看报警日志错误
由于报警日志文件很大,而每天都应该查看报警日志(查看有无“ORA-”,Error”,“Failed”等出错信息),故想找到一种比较便捷的方法,查看当天报警日志都有哪些错误. 在网上查了几天的资料,尝试 ...
- 修改Servlet模板
1.找到jar文件 查看MyEclipse根目录下的myeclipse.ini,找到Common文件夹的位置,打开文件夹..\Common\plugins 找到文件 com.genuitec.ecli ...