Android ListView出現異常解決辦法_Android

來源:互聯網
上載者:User

Android ListView 異常解決辦法:

ListView:The content of the adapter has changed but ListView did not receive a notification使用ListView時遇到如下的異常資訊:

10-26 18:30:45.085: E/AndroidRuntime(7323): 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. [in ListView(2131296280, class android.widget.ListView) with Adapter(class com.souapp.appmanager.ApkListAdapter)]

 其實我在listview的adapter添加完資料後,使用了handler去調用datper.notifyDataSetChanged();來通知listview顯示變化結果;

雖然自己很確定沒有多線程操作,但是有人說listview本來就是線程不安全的,這個不關心了,看了國外一個開發人員的方法很簡單。

解決方案1:

ListView.requestLayout(); 

Adatper.notifyDataSetChanged();

在你adpater更新前,調用listview的requestLayout(),這樣做無非就是彌補資料數量不一致導致報錯,雖然一個解決的好辦法。

 但是實際上用的時候我發現也會出問題,想了想最徹底的解決辦法

徹底解決方案:

把  listview的adapter資料更新和dapter.notifyDataSetChanged()方法的調用必須同時放到單獨一個線程裡,報錯基本是都是這個原因,有人把adapter裡的資料更新了,但是 dapter.notifyDataSetChanged() 放到一個單獨線程去更新,結果出現notifyDataSetChanged更新同步的問題

解決更新ListView資料時出現的問題分析總結:

  沒仔細讀的話,一眼看去就說說在非UI線程去更新了ListVIew的資料,然後下意識的認為是調用adapter.notifyDataSetChanged方法的調用被放到了非UI線程,仔細一看是說更新ListView的資料以及通知數據更新要放到同一個線程(主線程),是為了保持資料一致,adapter裡面一般是會存放一個數組,對那個資料的修改和調用notifyDataSetChanged方法要放到一起,而且是放到主線程,如果對資料的更新放到了子線程,notifyDataSetChanged的調用在主線程,notifyDataSetChanged的調用會預設把ListView綁定到主線程裡面了,這個時候子線程來更新資料的話就會出現在非UI線程修改UI線程的東西了。

這個問題也不是必崩潰,低版本崩的多,高版本好像崩潰得少

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.