in Android development, ListView very much, whether novice or veteran of many years of development for many years, are likely to encounter this exception:
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.
The abnormal reason is that your data set is changed, but does not call notifyDataSetChanged () method, but many people are very confused, it is called the notifyDataSetChanged () method, but reported the exception, I also developed more than two years of Android, or encountered this problem, I may be not enough strength, there is no rest this weekend, to solve this problem, it pays off, and finally solved
Let me describe the scene where this problem occurred:
Most of the time, we get the data from the network, and when we get it, we change the local data source, and then call the notifyDataSetChanged () method, and this process is not wrong.
We obtain the data will not directly access the UI thread inside the general, we will open a new thread, or with AsyncTask, there is some third party framework, such as okhttp, I use is okhttp, regardless of the approach, a new thread is in the run () method. AsyncTask is in the doInBackground () method, okhttp (onResponse) is in the method, we get the data are acquired in the inside of the thread, we will get the data changes directly with the binding of Adapter data set, the data set is changed in this thread inside, but we can't thread which calls notifyDataSetChanged () method, we must be on the UI thread which calls notifyDataSetChanged () method, then we will use the Handler (or runOnUiThread) method to deal with the problem, this time to, We change the data set in the thread, but called on the UI thread inside the notifyDataSetChanged () method, which is why the problem occurred, this anomaly does not necessarily occur from time to time, but will occasionally appear, once the error will not be prompted in one line, all internal abnormalities, and if you use the abnormal report third party, also won't be reported, is very difficult, very difficult to troubleshoot
the solution is to change the data set and call notifyDataSetChanged () method must not separate in the UI thread, and, according to the order, change the data set before, immediately call the notifyDataSetChanged () method, div>!!!
- Android call system camera system, 2 years ago1503 Views
- Android broadcast Daquan Intent Act2 years ago715 Views
- The use of Android zipalign2 years ago1149 Views
- Android development, from entry to 2 years ago847 Views
- Android commonly used AES encryptio2 years ago1546 Views
- My 2016-, my story with Android2 years ago1609 Views
- Android MVP mode2 years ago1517 Views
- When the Android Activity pop-up di2 years ago830 Views
- Android XML data analysis2 years ago1892 Views
- Android framework adds system servi2 years ago836 Views