我写了一个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();}@Overridepublic int getCount() {return devices == null ? 0 : devices.size();}

相对于原来,我做了两项改动:

1.将所有数据“完全”保存在adapter内部,即使有外部数据进入,也会用.clone()重新生成副本,保证了数据完全是由adapter维护的。

2.保证所有setDeviceList()/clearDeviceList()是从主线程里调用的,如何保证是从主线程中调用的呢:

  a.调用Activity.runOnUIThread()方法;

  b.使用Handler(其实这并不非常准确,因为Handler也可以运行在非UI线程);

  c.使用AsyncTask。

--------------------- 
原文:https://blog.csdn.net/ueryueryuery/article/details/20607845?utm_source=copy

解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题相关推荐

  1. 关于Adapter的The content of the adapter has changed but ListView did not receive a notification.问题分析

    1.问题描述 1 07-28 17:22:02.162: E/AndroidRuntime(16779): java.lang.IllegalStateException: The content o ...

  2. 解决java.lang.IllegalStateException: getOutputStream() has already been called for this response

    简单的说:用了流之后关掉即可. 下面详细说明: 出现了java.lang.IllegalStateException: getOutputStream() has already been calle ...

  3. java服务器错误怎么解决_如何解决java.lang.IllegalStateException:服务器处于错误状态异常...

    我正在尝试在核心 java项目(swing应用程序)中部署restful web-service.我正在使用jersy. 我已经搜索到谷歌的许多网站,但我无法找到为什么这附加. public clas ...

  4. 解决java.lang.IllegalStateException: Found multiple @SpringBootConfiguration annotated classes [Generi

    上报错信息 java.lang.IllegalStateException: Found multiple @SpringBootConfiguration annotated classes [Ge ...

  5. the content of the adapter has changed but listview did not

    http://dev.10086.cn/cmdn/wiki/index.php?doc-view-5432.html高手文章,要分享 这是我原来的代码 public class FileDeleteA ...

  6. 转:java.lang.IllegalStateException异常产生的原因及解决办法

    地址:http://jorton468.blog.163.com/blog/static/72588135201102441617287/ 问题描述: 错误类型大致为以下几种: java.lang.I ...

  7. JDK8 stream toMap() java.lang.IllegalStateException: Duplicate key异常解决(key重复)

    测试又报bug啦 接到测试小伙伴的问题,说是一个接口不返回数据了,好吧,虽然不是我写的接口任务落到头上也得解决,本地调试了一下,好家伙,直接抛了个异常出来,这又是哪位大哥喝醉了写的代码... Exce ...

  8. Android DialogFragment 遇到 java.lang.IllegalStateException: Fragment already added: 的解决方法

    Android DialogFragment 遇到 java.lang.IllegalStateException: Fragment already added: 的解决方法 参考文章: (1)An ...

  9. 解决IntelliJ IDEA报错:调用方法[manageApp]时发生异常java.lang.IllegalStateException: 启动子级时出错

    解决IntelliJ IDEA报错:调用方法[manageApp]时发生异常java.lang.IllegalStateException: 启动子级时出错 问题描述:   笔者将一个在 Tomcat ...

最新文章

  1. 实验进行中:.NET WebAssembly支持
  2. python完全新手教程-Python完全新手教程
  3. 【学习方法】学习心法总结之——如何平稳得开启数据之路
  4. oracle 导库时日志满了,oracle rac 11g 日志占满系统盘导致数据库down掉
  5. 【6】C++语法与数据结构之STL_list学生管理系统_链表外排序_函数指针
  6. linux设置程序循环,linux shell编程学习笔记(7)流程控制之循环结构
  7. 苹果手机没充满电就拔下,会对电池造成伤害吗?
  8. 暴雪战网客户端上使用的那些开源库!
  9. 工作中要注意拍照录像,证明工作完成
  10. IEEE期刊如何查找论文模板
  11. 有趣的c语言代码大全,分享一段有趣的小代码
  12. Unity3D 资源加载 Resources.Load
  13. 编程c语言中文图形代码,C语言图形编程代码
  14. CRM客户管理系统(Java)
  15. scrapy爬虫-拉勾网(学习交流)
  16. BBU+RRU基本介绍
  17. VT是什么?怎么打开教程
  18. css如何将彩色图片变为黑白图片
  19. python弹性碰撞次数圆周率_碰撞出来的圆周率(一)
  20. 2018年我国互联网网络安全态势综述

热门文章

  1. SEO优化之长尾词策略 - 站长之家
  2. 基于Springboot+vue的小米商城(源代码+数据库)056
  3. 解读PS3的四大战略
  4. Selective Search——Region Proposal的源头 (目标检测)(two-stage)(深度学习)(IJCV 2013)
  5. 阿里ECS无法telnet到smtp 25号端口的解决方案
  6. 移动开发实践项目:仿美柚界面基于Android的女性生理期管理APP
  7. gb编码转换utf8
  8. Photoshop史上最强更新,动动手指就能让AI替你修图
  9. 第1关:学习-Java集合类之List的LinkedList之特有方法的使用
  10. 如何生成像样的假数据