【转】解决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类改为:

[java] view plaincopy

  1. private class DeviceAdapter extends BaseAdapter {
  2. private LayoutInflater inflater;
  3. private ArrayList<Device> devices;
  4. public DeviceAdapter() {
  5. inflater = LayoutInflater.from(mContext);
  6. }
  7. @SuppressWarnings("unchecked")
  8. public void setDeviceList(ArrayList<Device> list) {
  9. if (list != null) {
  10. devices = (ArrayList<Device>) list.clone();
  11. notifyDataSetChanged();
  12. }
  13. }
  14. public void clearDeviceList() {
  15. if (devices != null) {
  16. devices.clear();
  17. }
  18. notifyDataSetChanged();
  19. }
  20. @Override
  21. public int getCount() {
  22. return devices == null ? 0 : devices.size();
  23. }
  24. 以下略)

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

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

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

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

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

  c.使用AsyncTask。

希望能帮到遇到同样问题的同学~

时间: 2024-10-12 21:22:06

【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题的相关文章

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

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

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: BeanFactory not initialized or already closed - call &#39;refresh&#39; before accessing beans via the ApplicationContext这个问题

今天在运行别人的SSH项目时,遇到了这个问题 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate compon

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也无法定位到准确的出错位置.检

开发问题及解决--java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <Horiz

暂时解决java.lang.IllegalStateException: It is invalid to call isReady() when the response has not been put into non-blocking mode

环境: 本机-apache-tomcat-9.0.0.M21 服务器-apache-tomcat-9.0.0.M9 错误复盘: 最初不知道是tomcat版本解决的,实现了一个下载PDF文件的功能,在action中使用SpringMVC提供的功能对OutputStream进行了close(这块内部逻辑暂不了解) 另外,通过切面@After注解记录用户访问日志,其中使用了一行代码JSON.toJSONString(arguments),结果程序报错 因为程序在本地没有这个问题,怀疑是tomcat版本

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数据改变. 在这里主要介绍第二种方式:

Android开发教程--关于Adapter的The content of the adapter has changed问题分析

我们在做android list开发的时候,有时候会遇到类似这样的问题: 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 th