Adapter报错:The content of the adapter has changed的解决
来源:互联网 发布:java怎么输入字符数组 编辑:程序博客网 时间:2024/05/15 12:11
在listView的编程中使用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也无法定位到准确的出错位置,后来仔细检查下发现,自己在后台的Background thread中直接调用adapter的notifyDataSetChanged()造成的。从网上查找资料需要将相关代码到Handler中处理,即解决此问题。
Handler的定义:主要接受子线程发送的数据, 并用此数据配合主线程更新UI。当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程),主线程为管理界面中的UI控件,进行事件分发,比如说,你要是点击一个 Button,Android会分发事件到Button上,来响应你的操作。 如果此时需要一个耗时的操作,例如,联网读取数据,或者读取本地较大的一个文件的时候,你不能把这些操作放在主线程中,如果你放在主线程中的话,界面会出现假死现象,如果5秒钟还没有完成的话,会收到Android系统的一个错误提示 "强制关闭"。 这个时候我们需要把这些耗时的操作,放在一个子线程中,因为子线程涉及到UI更新,Android主线程是线程不安全的,也就是说,更新UI只能在主线程中更新,子线程中操作是危险的。 这个时候,Handler就出现了来解决这个复杂的问题,由于Handler运行在主线程中(UI线程中),它与子线程可以通过Message对象来传递数据,这个时候,Handler就承担着接受子线程传过来的(子线程用sedMessage()方法传弟)Message对象,(里面包含数据) ,把这些消息放入主线程队列中,配合主线程进行更新UI。
继承Handler的子类:
//Handler类
private class MyHandler extends Handler{
public MyHandler(){
}
public MyHandler(Looper L){
super(L);
}
//子类重写此方法
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
//此处更新UI
if(msg.what==1){
MyAdapter adapter = new MyAdapter();
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
}
}
从DDMS中的log也无法定位到准确的出错位置,后来仔细检查下发现,自己在后台的Background thread中直接调用adapter的notifyDataSetChanged()造成的。从网上查找资料需要将相关代码到Handler中处理,即解决此问题。
Handler的定义:主要接受子线程发送的数据, 并用此数据配合主线程更新UI。当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程),主线程为管理界面中的UI控件,进行事件分发,比如说,你要是点击一个 Button,Android会分发事件到Button上,来响应你的操作。 如果此时需要一个耗时的操作,例如,联网读取数据,或者读取本地较大的一个文件的时候,你不能把这些操作放在主线程中,如果你放在主线程中的话,界面会出现假死现象,如果5秒钟还没有完成的话,会收到Android系统的一个错误提示 "强制关闭"。 这个时候我们需要把这些耗时的操作,放在一个子线程中,因为子线程涉及到UI更新,Android主线程是线程不安全的,也就是说,更新UI只能在主线程中更新,子线程中操作是危险的。 这个时候,Handler就出现了来解决这个复杂的问题,由于Handler运行在主线程中(UI线程中),它与子线程可以通过Message对象来传递数据,这个时候,Handler就承担着接受子线程传过来的(子线程用sedMessage()方法传弟)Message对象,(里面包含数据) ,把这些消息放入主线程队列中,配合主线程进行更新UI。
继承Handler的子类:
//Handler类
private class MyHandler extends Handler{
public MyHandler(){
}
public MyHandler(Looper L){
super(L);
}
//子类重写此方法
@Override
public void handleMessage(Message msg){
super.handleMessage(msg);
//此处更新UI
if(msg.what==1){
MyAdapter adapter = new MyAdapter();
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
}
}
- Adapter报错:The content of the adapter has changed的解决
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed
- Adapter报错:The content of the adapter has changed 解决方法
- Adapter报错:The content of the adapter has changed but ListView did not receive a
- Adapter报错:The content of the adapter has changed but ListView did not receive a
- Adapter报错:The content of the adapter has changed but ListView did not receive a
- 解决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 ListView...的问题
- C#判断文件是否被打开占用
- Android推送通知指南
- 100*100面试题
- 许多电子编程书籍网址,转载点点的
- eclipse 错误 java was started but returned exit code =-805306369 .
- Adapter报错:The content of the adapter has changed的解决
- 第1天
- 众望所归:13寸 Retina Macbook Pro性能测试曝光!
- C# 文件流操作 之 删除、复制、转移多个文件
- Struts1 配置文件详细说明
- tomcat7.0 用户名配置
- 嵌入式Linux C语言开发工具—调试器gdb详解
- linux远程管理的屠龙刀(二)
- getsockname与getpeername