码迷,mamicode.com
首页 > 其他好文 > 详细

记一个使用中常见的关于ListView和Adapter的BUG

时间:2015-05-13 10:14:41      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

   如题,代码中用到了ListView并用到了一个简单的SimpleAdapter来适配其中的List。

   其中在调试的时候会遇到弹错,log如下

     

E/MessageQueue-JNI( 2054): 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(2131361805, class android.widget.ListView) with Adapter(class com.roadrover.xxx.xxx)]
E/MessageQueue-JNI( 2054): 	at android.widget.ListView.layoutChildren(ListView.java:1538)
E/MessageQueue-JNI( 2054): 	at android.widget.AbsListView.onTouchEvent(AbsListView.java:3509)
E/MessageQueue-JNI( 2054): 	at android.view.View.dispatchTouchEvent(View.java:7127)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
E/MessageQueue-JNI( 2054): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
E/MessageQueue-JNI( 2054): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
E/MessageQueue-JNI( 2054): 	at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
E/MessageQueue-JNI( 2054): 	at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
E/MessageQueue-JNI( 2054): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
E/MessageQueue-JNI( 2054): 	at android.view.View.dispatchPointerEvent(View.java:7307)
E/MessageQueue-JNI( 2054): 	at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
E/MessageQueue-JNI( 2054): 	at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
E/MessageQueue-JNI( 2054): 	at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
E/MessageQueue-JNI( 2054): 	at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
E/MessageQueue-JNI( 2054): 	at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
E/MessageQueue-JNI( 2054): 	at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
E/MessageQueue-JNI( 2054): 	at android.os.MessageQueue.nativePollOnce(Native Method)
E/MessageQueue-JNI( 2054): 	at android.os.MessageQueue.next(MessageQueue.java:125)
E/MessageQueue-JNI( 2054): 	at android.os.Looper.loop(Looper.java:124)
E/MessageQueue-JNI( 2054): 	at android.app.ActivityThread.main(ActivityThread.java:4745)
E/MessageQueue-JNI( 2054): 	at java.lang.reflect.Method.invokeNative(Native Method)
E/MessageQueue-JNI( 2054): 	at java.lang.reflect.Method.invoke(Method.java:511)
E/MessageQueue-JNI( 2054): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/MessageQueue-JNI( 2054): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/MessageQueue-JNI( 2054): 	at dalvik.system.NativeStart.main(Native Method)
D/AndroidRuntime( 2054): Shutting down VM
       BUG的主要信息为

     

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(2131361805, class android.widget.ListView) with Adapter(class com.roadrover.xxx.xxx
       可见应该是我们在操作Adapter的时候有误,要么是多线程的时候在后台非UI线程处理了Adapter而没有更新ListView,要么是UI线程里更新了Adapter而没更新ListView。总之是更新Adapter和更新ListView没有匹配出现,于是仔细找了下确实发现了调用了数据集的clear操作,后面没有跟着调用adapter.notifyDataSetChanged()。都检查完后,继续调试,一切正常。

记一个使用中常见的关于ListView和Adapter的BUG

标签:

原文地址:http://blog.csdn.net/zhanghaofor/article/details/45689379

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!