ListView IllegalStateException
贴出源代码:
android.widget.ListView
...
if(mItemCount == 0){
resetList();
invokeOnItemScrollListener();
return;
}else if(mItemCount != mAdapter.getCount()){
throw new 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. "
+"Make sure your adapter calls notifyDataSetChanged() when its content changes. "
+"[in ListView(" +getId() +"", + getClass + ") with Adapter(" + mAdapter.getClass+ ")]");
}
...
今天看见这个异常百思不得其解,幸好在论坛上看见一位牛人的解析,瞬间明了。
原文分析:
普通情况下。上述异常一般发生在我们启动一个后台线程载入数据,同一时候在主线程(即UI线程)刷新ListView在显示新载入的内容。
我们的做法通常是:在后台线程中把载入的数据放入到一个List中,而在主线程中实例化Adapter,这个Adapter中所用到的List正是在后台线程中载入的那个List。
发生上述异常的代码思路是这样子的。请看代码:
首先,我们定义一个List全局变量,后台线程中载入的数据就放到这个list中(请注意我标了红色的list变量,问题就出在它身上):
private List<Map<String,Object>> list = null;
接着,我们会启动一个后台线程,用于载入数据:
class GetDataThread implements Runnable{//单独启动一个线程用于载入歌曲列表
@Override
public void run() {
list = new ArrayList<Map<String,Object>>();
//然后把搜索出来的数据放入到list中。
}
}
最后,我们会在主线程中刷新界面。刷新界面的代码。我们是要放到handler中处理的:
class RefreshLocalMusicListThread implements Runnable{
@Override
public void run() {
local_lv = (ListView)findViewById(R.id.local_musiclist);
SimpleAdapter adapter = new SimpleAdapter(LocalActivity.this,list,R.layout.local_music_list,new String[] {"local_name","local_size"}, new int[]{R.id.local_name,R.id.local_size});
local_lv.setAdapter(adapter);
LocalActivity.this.registerForContextMenu(local_lv);
handler.postDelayed(refreshThread, 10);
}
}
以上的思路,是会发生上述异常的!
以下请看我的分析:
当执行 SimpleAdapter adapter = new SimpleAdapter(LocalActivity.this,list,R.layout.local_music_list,new String[] {"local_name","local_size"}, new int[]{R.id.local_name,R.id.local_size});时集合list中数据与我们的listView是绑定在一起的了。
此时。,假如list中的数据有5条,即list.size()==5,这时与listView绑定的就是5条数据。可是,我们的后台线程还在执行,list中的数据会发生变化,然而我们的listView认定的就是之前仅仅有5条数据的list,可是这时的list中的数据已经不是5条了。就是这个冲突导致了上述的异常!!!发生在else
if()推断语句处
网上有这样一种解决方法(实际上解决不了问题):
在 adapter.notifyDataSetChanged() 之前调用listview.setVisibility(View.GONE);在adapter.notifyDataSetChanged() 之后调用listview.setVisibility(View.VISIBLE)
可是这是错误的!!
!
正确的解决方法是这种:
既然与listView绑定了的list发生了变化而没来得及通知listView导致了上述的异常,那我们就针对这一点,仅仅要listView与list绑定后,在listView显示之前不要让list发现变化即可了。做法有非常多种。我个人的做法是这样子的:
首先,定义一个独立的List:
private List<Map<String,Object>> data = null;
接着,在onCreate或者onResume中初始化它(当然,你也能够在每次用到它的时候初始化它。只是这样子会初始化非常多对象,浪费内存,不推荐):
data = new ArrayList<Map<String,Object>>();
然后,在创建adapter之前,把list中数据放入到集合data中。注意千万不要直接赋值:data = list(这是错误的。由于这样data也指向了list所在的内存地址,即data跟list是同一个对象。list改变的话data也跟着改变)。应该这么做:
data.clear();//要先清空data中的数据。避免把list中的数据反复放入data中。
data.addAll(list);//这样做。list中的数据就放入到data中,之后list在后台线程中改变,但data不会改变,这时,你再
SimpleAdapter adapter = new SimpleAdapter(LocalActivity.this,data,R.layout.local_music_list,new String[] {"local_name","local_size"}, new int[]{R.id.local_name,R.id.local_size});
listView与data绑定,就不会发生上述异常了!
总结来说。即创建一个缓存变量。存储的值是第一次查询得到数据 ,后台线程继续查询出来的数据不再使用。如此就保证了显示数据时不会报出异常。
ListView IllegalStateException的更多相关文章
- 关于viewpager 里嵌套 listview 同时实现翻页功能的“java.lang.IllegalStateException: The specified child..."异常处理
这几天做项目用到了ViewPager,因为它可以实现左右划动多个页面的效果,然后 再每个页面里使用ListView,运行时总是出现”PagerAdapter java.lang.IllegalStat ...
- 【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
原文网址:http://blog.csdn.net/ueryueryuery/article/details/20607845 我写了一个Dialog,Dialog中有一个ListView,想要点Li ...
- 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.layout ...
- java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
分析:android 4.2.X及以下的版本,addHeaderView必须在setAdapter之前,否则会抛出IllegalStateException. android 4.2.X(API 17 ...
- 深入理解使用ListView时ArrayAdapter、SimpleAdapter、BaseAdapter的原理
在使用ListView的时候,我们传给setAdapter方法的Adapter通常是ArrayAdapter.SimpleAdapter.BaseAdapter,但是这几个Adapter内部究竟是什么 ...
- android开发中难免遇到listview刷新数据出现异常
异常:java.lang.IllegalStateException: The content of the adapter has changed but ListView did not rece ...
- Android中ListView的各种显示效果
在android应用开发中,ListView是使用频率非常高的一个组件,基本上稍微复杂点的布局都会用到它,利用它可以让你的界面美观,有层次 .ListView可以用来作为数据显示的容器,也可以作为界面 ...
- Android ListView 进阶学习
1.使用ListView展示数据结构为二维数组的数据 当我们遇到数据结构是二维数组的需求的时候,我们会首先想到ListView,但是要想实现二维数组,会想到ListView里面嵌套ListView,但 ...
- ListView:The content of the adapter has changed but ListView did not receive a notification终极解决方法
使用ListView时遇到如下的异常信息: 10-26 18:30:45.085: E/AndroidRuntime(7323): java.lang.IllegalStateException: T ...
随机推荐
- paip.odbc DSN的存储与读取
paip.odbc DSN的存储与读取 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/atti ...
- 14.4.6 Configuring Thread Concurrency for InnoDB 配置Thread 并发
14.4.6 Configuring Thread Concurrency for InnoDB 配置Thread 并发 InnoDB 使用操作系统threads 来处理用户的事务请求.(事务可以执行 ...
- Common Lisp学习笔记(0):从SLIME开始 | 优哉·幽斋
Common Lisp学习笔记(0):从SLIME开始 | 优哉·幽斋 Common Lisp学习笔记(0):从SLIME开始
- Android内存管理
首先Android理机制相当复杂.想要讲清楚比較困难.其次对于绝大多数用户来说.仅仅关心内存够不够用,至于内存怎样管理的这样的技术细节,不是用户须要去考虑的,写这样一个专题有没有意义?毕竟我们是用手机 ...
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
- SimpleWiFi模块评估板
SimpleWiFi评估套件,发货清单: 1.评估版一块. 2.专用WiFi天线一根. 3.配套电源一个. 单模块 是60元,链接如下: http://item.taobao.com/i ...
- CreateThread、_beginthreadex和AfxBeginThread 的区别
CreateThread._beginthreadex和AfxBeginThread 创建线程好几个函数可以使用,可是它们有什么区别,适用于什么情况呢?参考了一些资料,写得都挺好的,这里做一些摘抄和整 ...
- C++ 多态性分析
编译 - 时间多态性--函数重载 编译后的中间代码(例如GCC产生.o文件.此时还不是汇编语言)函数名字有变化,看以下两个样例. void cc_show(const char*str) -& ...
- autoit 处理文件上传弹出框,并在JAVA中调用
Java 代码 //定义exe 文件存放的绝对路径 File file2 = new File("."); String command = file2.getCanonical ...
- Graphical Shell with WebShell - WebOS Internals
Graphical Shell with WebShell - WebOS Internals Graphical Shell with WebShell From WebOS Internals J ...