【轉】解決java.lang.IllegalStateException: The content of the adapter has changed but ListView...的問題

來源:互聯網
上載者:User

標籤:

原文網址:http://blog.csdn.net/ueryueryuery/article/details/20607845

我寫了一個Dialog,Dialog中有一個ListView,想要點ListView中的一項後,跳轉到另外一個Activity去。

但在使用時,會偶爾報出下面的錯誤:

 

02-21 14:54:28.928: E/AndroidRuntime(2846): FATAL EXCEPTION: main

02-21 14:54:28.928: E/AndroidRuntime(2846): 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(2131165196, class android.widget.ListView) with Adapter(class com.jovision.multiscreen.views.DeviceScanSelectDialog$DeviceAdapter)]
02-21 14:54:28.928: E/AndroidRuntime(2846):     at android.widget.ListView.layoutChildren(ListView.java:1510)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at android.widget.AbsListView.onTouchModeChanged(AbsListView.java:2077)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at android.view.ViewTreeObserver.dispatchOnTouchModeChanged(ViewTreeObserver.java:591)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at android.view.ViewRoot.ensureTouchModeLocally(ViewRoot.java:2095)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at android.view.ViewRoot.performTraversals(ViewRoot.java:809)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1861)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at android.os.Looper.loop(Looper.java:130)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at java.lang.reflect.Method.invoke(Method.java:507)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
02-21 14:54:28.928: E/AndroidRuntime(2846):     at dalvik.system.NativeStart.main(Native Method)

 

 

其中錯誤描述:

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.

的意思大體是,你的adapter的內容變化了,但是你的ListView並不知情。請保證你adapter的資料在主線程中變更!

 

知道了原因,改起來就好辦多了,我將我的adapter類改為:

 

[java] view plaincopy 
  1. private class DeviceAdapter extends BaseAdapter {  
  2.   
  3.     private LayoutInflater inflater;  
  4.     private ArrayList<Device> devices;  
  5.   
  6.     public DeviceAdapter() {  
  7.         inflater = LayoutInflater.from(mContext);  
  8.     }  
  9.   
  10.     @SuppressWarnings("unchecked")  
  11.     public void setDeviceList(ArrayList<Device> list) {  
  12.         if (list != null) {  
  13.             devices = (ArrayList<Device>) list.clone();  
  14.             notifyDataSetChanged();  
  15.         }  
  16.     }  
  17.   
  18.     public void clearDeviceList() {  
  19.         if (devices != null) {  
  20.             devices.clear();  
  21.         }  
  22.         notifyDataSetChanged();  
  23.     }  
  24.   
  25.     @Override  
  26.     public int getCount() {  
  27.         return devices == null ? 0 : devices.size();  
  28.     }  
  29. 以下略)  

 

相對於原來,我做了兩項改動:

1.將所有資料“完全”儲存在adapter內部,即使有外部資料進入,也會用.clone()重建副本,保證了資料完全是由adapter維護的。

2.保證所有setDeviceList()/clearDeviceList()是從主線程裡調用的,如何保證是從主線程中調用的呢:

  a.調用Activity.runOnUIThread()方法;

  b.使用Handler(其實這並不非常準確,因為Handler也可以運行在非UI線程);

  c.使用AsyncTask。

 

希望能幫到遇到同樣問題的同學~

 

【轉】解決java.lang.IllegalStateException: The content of the adapter has changed but ListView...的問題

聯繫我們

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