java.lang.IllegalStateException: The content of the adapter has changed but ListView..
2016-01-04 19:09
477 查看
错误日志:
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)]
错误分析:
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的数据在主线程中进行更改!
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。
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)]
错误分析:
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的数据在主线程中进行更改!
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。
相关文章推荐
- struts2 s:iterator标签
- 深入分析JavaWeb Item41 -- 邮件的发送与接收原理
- eclipse发布项目报错:Multiple Contexts hava a path of “/xxx“
- 解决java compiler level does not match the version of the installed java project facet
- java基础 —— properties 使用
- SpringMVC常用注解--【SpringMVC】
- jar命令成功完成 java -jar 命令却提示“没有主清单属性”!
- SpringMVC--常用注解(详解)
- [置顶] Java 23种设计模式
- 【慕课笔记】第一章 JAVA初体验 第6节 程序的移植
- Java就业前景怎么样?学了后好找工作吗?
- Java中的注解
- spring mvc 初始化错误
- Java——Java数组
- Java基础-学习笔记(二)
- java BIO实例——ServerSocket、Socket编程
- JAVA之数据溢出
- Java动态代理
- JAVA安装串口包
- Java编程思想读书笔记(一)