Analysis on the content of the adapter has changed in the adapter 1. Problem Description
1 07-28 17:22:02.162: E/AndroidRuntime(16779): 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(2131034604, class android.widget.ListView) with Adapter(class com.nodin.sarah.HeartListAdapter)] 2 3 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.ListView.layoutChildren(ListView.java:1555) 4 5 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.AbsListView.onLayout(AbsListView.java:2091) 6 7 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.View.layout(View.java:14785) 8 9 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewGroup.layout(ViewGroup.java:4631)10 11 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)12 13 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.View.layout(View.java:14785)14 15 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewGroup.layout(ViewGroup.java:4631)16 17 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)18 19 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)20 21 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.View.layout(View.java:14785)22 23 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewGroup.layout(ViewGroup.java:4631)24 25 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)26 27 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.View.layout(View.java:14785)28 29 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewGroup.layout(ViewGroup.java:4631)30 31 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)32 33 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.View.layout(View.java:14785)34 35 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewGroup.layout(ViewGroup.java:4631)36 37 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)38 39 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)40 41 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.View.layout(View.java:14785)42 43 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewGroup.layout(ViewGroup.java:4631)44 45 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)46 47 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)48 49 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)50 51 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.View.layout(View.java:14785)52 53 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewGroup.layout(ViewGroup.java:4631)54 55 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)56 57 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)58 59 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.View.layout(View.java:14785)60 61 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewGroup.layout(ViewGroup.java:4631)62 63 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1985)64 65 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1742)66 67 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)68 69 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)70 71 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)72 73 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.Choreographer.doCallbacks(Choreographer.java:562)74 75 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.Choreographer.doFrame(Choreographer.java:532)76 77 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)78 79 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.os.Handler.handleCallback(Handler.java:733)80 81 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.os.Handler.dispatchMessage(Handler.java:95)82 83 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.os.Looper.loop(Looper.java:137)84 85 07-28 17:22:02.162: E/AndroidRuntime(16779): at android.app.ActivityThread.main(ActivityThread.java:4998)86 87 07-28 17:22:02.162: E/AndroidRuntime(16779): at java.lang.reflect.Method.invokeNative(Native Method)88 89 07-28 17:22:02.162: E/AndroidRuntime(16779): at java.lang.reflect.Method.invoke(Method.java:515)90 91 07-28 17:22:02.162: E/AndroidRuntime(16779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)92 93 07-28 17:22:02.162: E/AndroidRuntime(16779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)94 95 07-28 17:22:02.162: E/AndroidRuntime(16779): at dalvik.system.NativeStart.main(Native Method)96 97 07-28 17:22:02.162: W/ActivityManager(588): Force finishing activity
2. In the recurrence scenario, listview and adapter are used to dynamically add and delete data lists. The initial data is divided into two parts: local and network. Therefore, when initializing the adapter data, first add the local data to the container. At the same time, initiate a network request and append it to the container after loading. The problem occurs: when the network request is complete and data is appended, the above exception is thrown. 3. Cause Analysis exception explanation: the data content of the adapter has changed, but the listview has not received the notification. Make sure that the data content of the adapter is not modified in the background thread, but in the UI thread. Make sure that the notifydatasetchanged () method is called when the data content of the adapter changes. Regardless of the exception content, first query the android source code to see where the exception was thrown. The layoutchildren () method of listview contains the following methods:
1 // Handle the empty set by removing all views that are visible 2 // and calling it a day 3 if (mItemCount == 0) { 4 resetList(); 5 invokeOnItemScrollListener(); 6 return; 7 } else if (mItemCount != mAdapter.getCount()) { 8 throw new IllegalStateException("The content of the adapter has changed but " 9 + "ListView did not receive a notification. Make sure the content of "10 + "your adapter is not modified from a background thread, but only "11 + "from the UI thread. [in ListView(" + getId() + ", " + getClass()12 + ") with Adapter(" + mAdapter.getClass() + ")]");13 }
In other words, this exception is thrown when the Count value of the data cached by listview is different from that of adapter. getcount () in listview. Based on the exception interpretation at the beginning, it can be determined that it is definitely a problem of dynamic updating of adapter data. Check your code carefully and find that there is a problem with data update: After the network request is complete, you directly call the custom method adddata (list) of the adapter to update the data, however, the policydatasetchanged () method is missing (I will remove it ...). In this case, the problem can be determined. Add notifydatasetchanged () to solve the problem. 4. the specific reason for the solution is complex, but it must be related to the adapter data update. Check your code from the following aspects:
- Make sure that the notifydatasetchanged () method is called to notify listview after the adapter data is updated.
- Data Updates and notifydatasetchanged () are placed in the UI thread and must be executed in synchronous sequence and cannot be asynchronous.
- Check whether the returned value of the getcount () method is correct.