Android開發:java.lang.IllegalStateException報錯

來源:互聯網
上載者:User

標籤:

 

常見於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報錯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.