java.lang.IllegalStateException: The content of the adapter has changed but
来源:互联网 发布:数据库配置文件 编辑:程序博客网 时间:2024/06/16 19:51
使用listview 的时候出现如下错误:
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.
检查adapter.notifyDataSetChanged();是否在主线程执行;
我遇到的情况是我在handle里面执行更新操作还是会出现上面那个错误,而且只是偶尔报错。
private Handler myHandler = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case Constant.MSG_SUCCESS:
if (adapter != null) {
adapter.notifyDataSetChanged();
}
break;
default:
break;
}
return false;
}
});
后来再网上查资料说handle不一定会在主线程。又改成这样:
private Handler myHandler = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case Constant.MSG_SUCCESS:
if (adapter != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
}
break;
default:
break;
}
return false;
}
});
还是会报错;后来想了很久发现问题原因,举个简单的例子
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 10; i++){
String str = "s"+i;
list.add(str);
}
myHandler.sendEmptyMessage(Constant.MSG_SUCCESS);
}
}).start();
list是adapter传递的集合数据是个全局变量。我们在子线程直接这样添加数据数量改变了有时候系统会自动更新ui导致报错。
只要改成这样就好了:
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
List<String>listTemp = new ArrayList<String>();
for(int i = 0; i < 10; i++){
String str = "s"+i;
listTemp.add(str);
}
Message msg = Message.obtain();
msg.obj = listTemp;
msg.what = Constant.MSG_SUCCESS;
myHandler.sendMessage(msg);
}
}).start();
private Handler myHandler = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case Constant.MSG_SUCCESS:
list.addAll((List<String>)msg.obj);
if (adapter != null) {
adapter.notifyDataSetChanged();
}
break;
default:
break;
}
return false;
}
});
这样我们把添加数据都放在主线程,这就万无一失了
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but
- 解决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
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receiv
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- 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 receiv
- java.lang.IllegalStateException: The content of the adapter has changed but
- 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
- 解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
- 解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
- java.lang.IllegalStateException: The content of the adapter has changed but
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java获取文件大小
- 考试篇(5.2) NSE4 题库 17. 单点登录 ❀ 飞塔 (Fortinet) 网络安全专家
- Java 并发专题 :闭锁 CountDownLatch 之一家人一起吃个饭
- 第一步 javaweb开发工具说明
- 底部弹出DialogFragment中使用ViewPager,ViewPager中使用Fragment出现Fragment does not have a view错误
- java.lang.IllegalStateException: The content of the adapter has changed but
- 卷积神经网络卷积层和池化层学习、权值共享!!
- ACM-二分贪心R-18
- Windows下Ftp搭建和Java中的使用个人总结
- 有用的网站
- post请求
- 简单使用AsyncTask
- java中的值传递和引用传递问题
- Calendar类