1. 程式人生 > >關於Adapter資料變化但未及時通知ListView資料變化引起的crash問題

關於Adapter資料變化但未及時通知ListView資料變化引起的crash問題

錯誤資訊:

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(16908298, class com.huawei.fans.pulltorefresh.library.PullToRefreshListView$InternalListViewSDK9) with Adapter(class android.widget.HeaderViewListAdapter)]

問題出現原因分析和解決方法,在http://www.daxueit.com/article/4671.html中都有說明,這裡mark下.

解決方案總結:將修改adapter 資料和呼叫notifyDataSetChanged()方法都放在主執行緒中線性執行,防止多執行緒排程引起概率性notifyDataSetChanged() 通知延時,從而crash。