標籤:
常見於ListView列表重新整理資料時,更改UI。
LOG:
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。
DDMS中的log無法定位到準確的出錯位置。檢查錯誤可從以下幾點入手:
1、檢查Thread,確定沒有在Background thread中直接調用adapter,如果有,請移除相關代碼到Handler中處理;
2、盡量將資料放在adapter類中管理,不需要的時候清除資訊(勤寫clear()),及時用notifyDataSetChanged()重新整理;
3、在Activity或者Fragment合適的位置(onPause/onStop)要及時檢查thread,有adapter資料處理相關的應馬上停止;
4、這個錯誤經常出現在Activity休眠起來之後,主要還是使用adapter不太小心造成的。如果實在找不到原因,在onPause()函數中停止所有的background thread,並且在onResume()函數最前面清空adapter中的資料,並且adapter.notifyDataSetChanged()。然後重新更新載入資料,這樣一般可以解決問題。
我所犯差錯是:
提前將adapter的資料全清空了adapter.getDateList.clear(),且並沒有及時調用adapter.notifyDataSetChanged(),導致ListView沒有資料而拋java.lang.IllegalStateException這個異常。
使用者體驗:重新整理頁面或下拉重新整理,手指點擊任意UI,導致崩潰。
解決做法:將adapter.getDateList.clear()與adapter.notifyDataSetChanged()寫在一起
Android開發:java.lang.IllegalStateException報錯