天天看点

解决The content of the adapter has changed but ListView did not receive a notification

问题分析:

log的信息很简单,就是在改动ListView内容后,没有调用notifyDataSetChanged函数来更新,才导致的异常。

- ::: E/AndroidRuntime(): 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. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(, class com.handmark.pulltorefresh.library.PullToRefreshListView$b) with Adapter(class android.widget.HeaderViewListAdapter)]
- ::: E/AndroidRuntime():    at android.widget.ListView.layoutChildren(ListView.java:)
- ::: E/AndroidRuntime():    at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:)
- ::: E/AndroidRuntime():    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:)
- ::: E/AndroidRuntime():    at android.view.Choreographer.doCallbacks(Choreographer.java:)
- ::: E/AndroidRuntime():    at android.view.Choreographer.doFrame(Choreographer.java:)
- ::: E/AndroidRuntime():    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:)
- ::: E/AndroidRuntime():    at android.os.Handler.handleCallback(Handler.java:)
- ::: E/AndroidRuntime():    at android.os.Handler.dispatchMessage(Handler.java:)
- ::: E/AndroidRuntime():    at android.os.Looper.loop(Looper.java:)
- ::: E/AndroidRuntime():    at android.app.ActivityThread.main(ActivityThread.java:)
- ::: E/AndroidRuntime():    at java.lang.reflect.Method.invokeNative(Native Method)
- ::: E/AndroidRuntime():    at java.lang.reflect.Method.invoke(Method.java:)
- ::: E/AndroidRuntime():    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:)
- ::: E/AndroidRuntime():    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:)
- ::: E/AndroidRuntime():    at dalvik.system.NativeStart.main(Native Method)
           

解决方法:

1.首先保证不在线程中调用notifyDataSetChanged函数。

2.在每次涉及到修改ListView内容时,一定要调用notifyDataSetChanged即可。

继续阅读