1. 程式人生 > >解決ListView的The content of the adapter has changed but ListView did not receive a notification崩潰問題

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

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

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(2131296532, class android.widget.ListView) with Adapter(class android.widget.HeaderViewListAdapter)]


後來發現是資料填充和資料變更提醒不在同一執行緒導致,

原始程式碼如下:

// 向伺服器請求資料
private fun requestData(location: String) {
    Service.queryWeather(UrlConstant.WEATHER_URL, location, KeyConstant.WEATHER_KEY) { state, data ->
        swipe_layout.isRefreshing = false
	if (!state) [email protected]
        val response = JSON.parseObject(data, WeatherResponse::class.java)
        if (response.heWeather6[0].status != "ok") 
[email protected]
list.clear() list.addAll(response.heWeather6[0].daily_forecast) updateViews(response.heWeather6[0]) } } // 更新介面 private fun updateViews(data: WeatherResponse.HeWeather) { runOnUiThread { adapter.notifyDataSetChanged() tv_location.text = "省份:${data.basic.admin_area} 市級:${data.basic.parent_city} 地區:${location}" tv_latlng.text = "經度:${data.basic.lat} 緯度:${data.basic.lon}" tv_update_time.text = "更新時間:${data.update.loc}" } }

修正後代碼如下:

// 向伺服器請求資料
private fun requestData(location: String) {
    Service.queryWeather(UrlConstant.WEATHER_URL, location, KeyConstant.WEATHER_KEY) { state, data ->
        swipe_layout.isRefreshing = false
        if (!state) [email protected]
        val response = JSON.parseObject(data, WeatherResponse::class.java)
        if (response.heWeather6[0].status != "ok") [email protected]
        updateViews(response.heWeather6[0])
    }
}

// 更新介面
private fun updateViews(data: WeatherResponse.HeWeather) {
    runOnUiThread {
        list.clear()
        list.addAll(data.daily_forecast)
        adapter.notifyDataSetChanged()
        tv_location.text = "省份:${data.basic.admin_area}  市級:${data.basic.parent_city}  地區:${location}"
        tv_latlng.text = "經度:${data.basic.lat}  緯度:${data.basic.lon}"
        tv_update_time.text = "更新時間:${data.update.loc}"
    }
}

也就是說,只需要保證資料填充和adapter.notifyDataSetChanged()在同一執行緒即可,(另外:不能在子執行緒更新UI),bug解決

相關推薦

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

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

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

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

IllegalStateException: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. Make sure the

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,發現這個錯誤偶爾會出現。特別是做壓力測

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

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

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

(轉)關於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

Linux SSH 遠端登入錯誤解決辦法 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

最近在登入一臺機器的時候,發現無法等入了,出現以下錯誤: 於是就度娘(不得不說搜尋引擎是個好東西 -- ),找了好久發現有篇文章裡面提到.ssh/known_hosts檔案,檢視果然有這個檔案: 就想這檔案是個啥東東,待一查,才明白了,原來known_hosts是記錄遠端主機的公鑰的檔

Linux SSH 遠端登入錯誤解決辦法 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

      最近在登入一臺機器的時候,發現無法等入了,出現以下錯誤: 於是就度娘(不得不說搜尋引擎是個好東西),找了好久發現有篇文章裡面提到.ssh/known_hosts檔案,檢視果然有這個檔案: 就想這檔案是個啥東東,待一查,才明白了,原來known_hosts是記錄

eclipse指定jdk版本啟動,解決Version XXXX of the JVM is not suitable

可以解決Version XXXX of the JVM is not suitable for this product.Version:XXXXXX or greater is required。

解決Fiddler "creation of the root certificate was not successful”的問題

安裝證書,如果提示沒有證書(8888是你在Tool->Fiddler Option->Connections設定的Fiddler監聽埠) cd "d:\Program Files\Fiddler" makecert.exe -r -ss my -n "C

Android studio The versions of the Android Gradle plugin and Gradle are not compatible.

錯誤: The versions of the Android Gradle plugin and Gradle are not compatible. 原因是:Android的Gealle外掛和Gradle的版本是不相容的。 需要修改工程裡的bulid.grade的 cla

Input ‘split_dim’ of ‘Split’ Op has type float32 that does not match expected type of int32 報錯

#原來是這樣的: This is because in Tensorflow versions < 0.12.0 the split function takes the arguments as: x = tf.split(0, n_steps, x) # tf.split(axis, nu

Android Studio新增AAR檔案報錯:the versions of the android gradle plugin and gradle are not compatible,plea

   在專案新增AAR檔案報錯:Error:Execution failed for task ':app:prepareAlivcPlayerLibrary'. > Could not expand ZIP '/xxxx/app/libs/AlivcPlayer.a

解決警告: Setting property 'source' to 'org.eclipse.jst.jee.server_:' did not find a matching property.的方法

頁面 .com img localhost set 框架 ID org fin 今天第一次搭建struts2框架,跟著網上的教程導入對應的jar包之後就開始寫登錄的jsp頁面,但是運行時出現了問題, 瀏覽器顯示“The requested resource is not a

[jnhs]使用netbeans生成的webapp釋出到tomcat是需要改名字的,不然就是404Description The origin server did not find a current representation for the target resource or is not

   第一次使用tomcat釋出webapp 遇到404錯誤  Description The origin server did not find a current representation for the target resource or is not will

打包造成The packaging for this project did not assign a file to the build artifact -> [Help 1]

今天在家打包遇見The packaging for this project did not assign a file to the build artifact 仔細看了一下發現今天腦抽了在Plugins下打包...打包應該在Lifecycle裡的 兩處打包,而非Plugin

mvn 打jar包異常:The packaging for this project did not assign a file to the build artifact -> [Help 1]

idea maven打包 install 報錯,資訊如下: 這是說找不到外掛的包,其實不是找不到這個打包外掛,而是自己的專案沒有從maven倉庫里加載這個包到專案裡, idea中打包maven專案,有兩個install;其實本人很菜鳥,一直就用Plugins下的ins