Android,ListView IllegalStateException:“适配器的内容发生了变化,但ListView没有收到通知" [英] Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification"

查看:26
本文介绍了Android,ListView IllegalStateException:“适配器的内容发生了变化,但ListView没有收到通知"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的事:运行一个后台线程,计算 ListView 内容并部分更新 ListView,同时计算结果.

What I want to do: run a background thread which calculates ListView contents and update ListView partially, while results are calculated.

我知道我必须避免的事情:我不能弄乱后台线程中的 ListAdapter 内容,所以我继承了 AsyncTask 并从 onProgressUpdate 发布结果(向适配器添加条目).我的适配器使用结果对象的 ArrayList,对这些 arraylist 的所有操作都是同步的.

What I know I have to avoid: I cannot mess with ListAdapter contents from background thread, so I inherited AsyncTask and publish result (add entries to adapter) from onProgressUpdate. My Adapter uses ArrayList of result objects, all operations on those arraylists are synchronized.

其他人的研究:有非常有价值的数据这里.我几乎每天都会遇到大约 500 名用户的崩溃,当我在 onProgressUpdate 中添加 list.setVisibility(GONE)/trackList.setVisibility(VISIBLE) 块时,崩溃降低了 10 倍,但没有消失.(建议在 answer 中)

Research of other people: there is very valuable data here. I also suffered from almost daily crashes for group of ~500 users, and when I added list.setVisibility(GONE)/trackList.setVisibility(VISIBLE) block in onProgressUpdate, crashes lowered by a factor of 10 but not disappeared. (it was suggested in answer )

我有时会得到什么:请注意,这种情况很少发生(每周一次,对于 3.5k 用户之一).但我想完全摆脱这个错误.这是部分堆栈跟踪:

What I got sometimes: please notice, it happens really rarely (once a week for one of 3.5k users). But I'd like to get rid of this bug completely. Here is partial stacktrace:

`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(2131296334, class android.widget.ListView) with Adapter(class com.transportoid.Tracks.TrackListAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1432)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:2062)
at android.widget.ListView.onTouchEvent(ListView.java:3234)
at android.view.View.dispatchTouchEvent(View.java:3709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
[...]

帮助?不再需要,见下文

最终答案:事实证明,我每 5 次插入调用一次 notifyDataSetChanged 以避免闪烁和突然的列表更改.不能这样做,当基本列表发生变化时总是通知适配器.这个错误现在对我来说早已消失了.

FINAL ANSWER: As it turned out, I was calling notifyDataSetChanged every 5 insertions to avoid flickering and sudden list changes. It cannot be done such way, always notify adapter when base list changes. This bug it long gone for me now.

推荐答案

我也遇到了同样的问题.

I had the same issue.

我在 UI 线程之外向我的 ArrayList 添加项目.

I was adding items to my ArrayList outside the UI thread.

解决方案:我都做了,添加项目并在UI线程中调用notifyDataSetChanged().

Solution: I have done both, adding the items and called notifyDataSetChanged() in the UI thread.

这篇关于Android,ListView IllegalStateException:“适配器的内容发生了变化,但ListView没有收到通知"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆