1. 程式人生 > >Adapter報錯:The content of the adapter has changed的解決

Adapter報錯:The content of the adapter has changed的解決

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也無法定位到準確的出錯位置,後來仔細檢查下發現,自己在後臺的Background thread中直接呼叫adapter的notifyDataSetChanged()造成的。從網上查詢資料需要將相關程式碼到Handler中處理,即解決此問題。
Handler的定義:
主要接受子執行緒傳送的資料, 並用此資料配合主執行緒更新UI。當應用程式啟動時,Android首先會開啟一個主執行緒 (也就是UI執行緒),主執行緒為管理介面中的UI控制元件,進行事件分發,比如說,你要是點選一個 Button,Android會分發事件到Button上,來響應你的操作。  如果此時需要一個耗時的操作,例如,聯網讀取資料,或者讀取本地較大的一個檔案的時候,你不能把這些操作放在主執行緒中,如果你放在主執行緒中的話,介面會出現假死現象,如果5秒鐘還沒有完成的話,會收到Android系統的一個錯誤提示  "強制關閉"。 這個時候我們需要把這些耗時的操作,放在一個子執行緒中,因為子執行緒涉及到UI更新,Android主執行緒是執行緒不安全的,也就是說,更新UI只能在主執行緒中更新,子執行緒中操作是危險的。 這個時候,Handler就出現了來解決這個複雜的問題,由於Handler執行在主執行緒中(UI執行緒中),它與子執行緒可以通過Message物件來傳遞資料,這個時候,Handler就承擔著接受子執行緒傳過來的(子執行緒用sedMessage()方法傳弟)Message物件,(裡面包含資料)  ,把這些訊息放入主執行緒佇列中,配合主執行緒進行更新UI。

繼承Handler的子類:
//Handler類
    private class MyHandler extends Handler{
        public MyHandler(){
            
        }
        public MyHandler(Looper L){
            super(L);
        }
        //子類重寫此方法
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            //此處更新UI
            if(msg.what==1){
                MyAdapter adapter = new MyAdapter();
                adapter.notifyDataSetChanged();
                listView.setAdapter(adapter);
            }
            
        }
    }

相關推薦

AdapterThe content of the adapter has changed解決

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

UNION關鍵字ERROR 1222 (21000): The used SELECT statements have a different number of columns

今天進行總結時,發現這個錯誤,後來,發現這是由於union 造成。 在使用union時,有以下規則。 1.必須由兩條或者兩條以上的select語句組成,語句之間用關鍵字union分隔 2.每

eclipse啟動Could not create the java virtual machine

報錯 解壓 java 開發 升級 最新 分享 src info 用maven、springboot開發時,安裝了當時最新版的eclipse(3.5.5)。eclipse解壓版的非常方便,想先安裝了看看。暫時沒有升級其他軟件。 打開的時候報錯: 原因其實就是還木有升級新版

TensorFlow測試程式異常FutureWarning: Conversion of the second argument of issubdtype from `float` to `np

使用安裝好的tensorflow-gpu 進行程式測試時出現異常: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In futu

npm install總是unable to verify the first certificate(無法驗證第一證書)

解決方法一: 取消ssl驗證:npm config set strict-ssl false 如果還沒成功,則將npm源更換為國內映象:npm config set registry http://registry.cnpmjs.org/ npm config set registr

使用BouncyCastleProvider類JCE cannot authenticate the provider BC

java.lang.SecurityException: JCE cannot authenticate the provider BC  當出現這個錯誤,網上一般都是要修改jre/lib/sercure下的檔案的,這個太麻煩了。問題的根源並不是jre的錯,因此沒必要這麼搞。 出現這個

dockerCannot connect to the Docker daemon. Is the docker daemon running on this host?

docker這種報錯一般情況都是docker未啟動對於這種情況只用重啟docker就行了: service docker restart   還要一種情況則是docker配置檔案出錯按照提示檢視報錯,並找到相應位置進行更改: systemctl status do

PHP Deprecated: Methods with the same name as their class will not be constructor...

5.6 name soa 項目 徹底 truct 升級 使用 The 報著個錯的原因是 最近把一個項目從php5.6升級到了php7 報如下錯誤: Deprecated: Methods with the same name as their class will not

部署CM(5)hive配置連線mysqlAble to find the Database server, but not the specified database.

1.問題描述 主機無法連線mysql資料庫,連線報錯 Able to find the Database server, but not the specified database. Please check if the database name is correct and make

The content of the adapter has changed but ListView did not receive a notification的解決方案

在開發過程中,使用ListView時,發現這個錯誤偶爾會出現。特別是做壓力測試的時候,不停的點選重新整理,更容易出現這個錯誤。 在網上找了一下關於這個問題的解決辦法,說的是直接備份一份資料來源,我根據

iOS 沒有檢視許可權the file "xxx.app" couldn't be opened because you don't have permission to view it.

iOS the file "xxx.app" couldn't be opened because you don't have permission to view it.問題解決方法彙總 (1) 最近重構專案,增加target來區分環境進行差異化編譯配置。 報

解決ListView的The content of the adapter has changed but ListView did not receive a notification崩潰問題

最近在寫一個聚合工具箱的時候,添加了滑動返回功能,使用ListView時(不要問我為啥不用recycleview,小工具嘛,主要是邏輯,頁面都是次要的),遇到如下崩潰問題 java.lang.IllegalStateException: The content of t

mysql啟動Starting MySQL... ERROR! The server quit without updating PID file

mysql啟動時報錯:Starting MySQL... ERROR! The server quit without updating PID file (/opt/mysql/data/mysql.pid) 的解決方法:1、可能是/opt/mysql/data/資料目錄m

ListView資料來源不同步問題The content of the adapter has changed but ListView did not receive a notification.

最近專案中出現了很多下面的異常: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Mak

非同步載入Listview The content of the adapter has changed but ListView did not receive a notification

錯誤 The content of the adapter has changed but ListView did not receive a notification 原因 在Android開發過程中,使用了大量的ListView,發現這個錯誤偶爾會出現。特別是做壓力測

(轉)關於AdapterThe content of the adapter has changed問題分析

1 07-28 17:22:02.162: E/AndroidRuntime(16779): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a no

tomcat啟動Unable to complete the scan for annotations for web application

tomcat啟動報錯,具體報錯資訊如下: Unable to complete the scan for annotations for web application [/Ftrans] due to a StackOverflowError. Possible root

Listview更新資料時崩潰The content of the adapter has changed but did not receive a notification.

說明:在工作中遇到的問題記錄下來,歡迎批評和指正~ 1、問題 在listView上下拉重新整理或者滑動過程中經常碰到這個復現率比較高的崩潰問題 E/AndroidRuntime(16779): j

failed to get the task for process XXX(解決方案)

引人:     iOS真機除錯程式,報如下錯誤資訊: failed to get the task for process XXX 原因: 證書問題,project和targets的證書都必

解決mysql- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ'

_for tran contains column schema mysql eat table express mysql執行報錯: - Expression #1 of ORDER BY clause is not in GROUP BY clause and cont