关于Adapter的The content of the adapter has changed问题分析 (转载)

基于TI MSPM0的PS2双轴按键摇杆模块ADC+GPIO驱动移植实战 本文详细介绍了在TI MSPM0微控制器上驱动PS2双轴按键摇杆模块的完整实战流程。通过SysConfig图形化工具配置ADC与GPIO引脚,并编写了包含ADC采样、按键检测及数据处理的驱动代码,实现了对摇杆X/Y轴模拟量及按键数字量的稳定读取,为嵌入式控制项目提供了可直接移植的解决方案。 阅读详情

写得很详细, 转载下:https://www.cnblogs.com/monodin/p/3874147.html

1、问题描述

复制代码

 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 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(2131034604, class android.widget.ListView) with Adapter(class com.nodin.sarah.HeartListAdapter)]
 2 
 3 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.ListView.layoutChildren(ListView.java:1555)
 4 
 5 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.AbsListView.onLayout(AbsListView.java:2091)
 6 
 7 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.View.layout(View.java:14785)
 8 
 9 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewGroup.layout(ViewGroup.java:4631)
10 
11 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
12 
13 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.View.layout(View.java:14785)
14 
15 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewGroup.layout(ViewGroup.java:4631)
16 
17 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
18 
19 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
20 
21 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.View.layout(View.java:14785)
22 
23 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewGroup.layout(ViewGroup.java:4631)
24 
25 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
26 
27 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.View.layout(View.java:14785)
28 
29 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewGroup.layout(ViewGroup.java:4631)
30 
31 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
32 
33 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.View.layout(View.java:14785)
34 
35 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewGroup.layout(ViewGroup.java:4631)
36 
37 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
38 
39 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
40 
41 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.View.layout(View.java:14785)
42 
43 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewGroup.layout(ViewGroup.java:4631)
44 
45 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
46 
47 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
48 
49 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
50 
51 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.View.layout(View.java:14785)
52 
53 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewGroup.layout(ViewGroup.java:4631)
54 
55 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
56 
57 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
58 
59 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.View.layout(View.java:14785)
60 
61 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewGroup.layout(ViewGroup.java:4631)
62 
63 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1985)
64 
65 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1742)
66 
67 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
68 
69 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
70 
71 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
72 
73 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.Choreographer.doCallbacks(Choreographer.java:562)
74 
75 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.Choreographer.doFrame(Choreographer.java:532)
76 
77 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
78 
79 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.os.Handler.handleCallback(Handler.java:733)
80 
81 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.os.Handler.dispatchMessage(Handler.java:95)
82 
83 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.os.Looper.loop(Looper.java:137)
84 
85 07-28 17:22:02.162: E/AndroidRuntime(16779):   at android.app.ActivityThread.main(ActivityThread.java:4998)
86 
87 07-28 17:22:02.162: E/AndroidRuntime(16779):   at java.lang.reflect.Method.invokeNative(Native Method)
88 
89 07-28 17:22:02.162: E/AndroidRuntime(16779):   at java.lang.reflect.Method.invoke(Method.java:515)
90 
91 07-28 17:22:02.162: E/AndroidRuntime(16779):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
92 
93 07-28 17:22:02.162: E/AndroidRuntime(16779):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
94 
95 07-28 17:22:02.162: E/AndroidRuntime(16779):   at dalvik.system.NativeStart.main(Native Method)
96 
97 07-28 17:22:02.162: W/ActivityManager(588):   Force finishing activity  

复制代码

 

2、复现场景

使用ListView和Adapter实现动态增删数据列表功能,初始化数据分为两部分:本地和网络。所以在Adapter的数据初始化的时候,先讲本地数据添加到了容器内。同时发起网络请求,等加载完毕后追加到容器内。

问题出现在:当网络请求完毕后追加数据的时候,抛出上述异常。

 

3、原因分析

Exception解读:

        Adapter的数据内容已经改变,但是ListView却未接收到通知。要确保不在后台线程中修改Adapter的数据内容,而要在UI Thread中修改。确保Adapter的数据内容改变时一定要调用notifyDataSetChanged()方法。

 

且不管Exception内容,先查询Android源码看看该Exception是从哪里抛出来的。

在ListView的layoutChildren()方法里有如下一段方法:

复制代码

 1 // Handle the empty set by removing all views that are visible
 2 // and calling it a day
 3 if (mItemCount == 0) {
 4     resetList();
 5     invokeOnItemScrollListener();
 6     return;
 7 } else if (mItemCount != mAdapter.getCount()) {
 8     throw new IllegalStateException("The content of the adapter has changed but "
 9             + "ListView did not receive a notification. Make sure the content of "
10             + "your adapter is not modified from a background thread, but only "
11             + "from the UI thread. [in ListView(" + getId() + ", " + getClass()
12             + ") with Adapter(" + mAdapter.getClass() + ")]");
13 }

复制代码

 

亦即,当ListView缓存的数据Count和ListView中Adapter.getCount()不等时,会抛出该异常。

 

结合开头的异常解读,可以断定肯定是Adapter数据动态更新的问题。仔细检查了自己的代码:

 

当网络请求完毕后,直接在网络线程(非UI线程)里调用了在Adapter中新增的自定义方法addData(List)更新数据,而addData(List)方法内更新换完数据后,通过Handler发送Message的策略调用Adapter的notifyDataSetChanged()方法通知更新。

 

这么一来,并不能保证Adapter的数据更新时,立马调用notifyDataSetChanged()通知ListView,这两个线程之间的时间差引起的数据不同步,导致ListView的layoutChildren()中访问Adapter的getCount()方法时,Adapter内已经是最新数据源,而ListView内的缓存数据Count仍是旧数据的Count,该问题最终原因终于浮出水面。

 

4、解决方案

在本例中,解决方案是:把addData(List)方法内更新数据的代码挪出来,和notifyDataSetChanged()方法一同放在Handler里,保证数据更新时及时通知ListView。

 

为了尽量避免该问题,以后编程尽量从如下几个方面检查自己的代码:

  • 确保Adapter的数据更新后一定要调用notifyDataSetChanged()方法通知ListView
  • 数据更新和notifyDataSetChanged()放在UI线程内,且必须同步顺序执行,不可异步
  • 仔细检查确认getCount()方法返回值是否正确
各种加法器的比对分析与Verilog实现(3) 了Kogge-Stone加法器和brent-kung加法器,本文将用Verilog代码进行实现。 阅读详情

相关推荐

吃透Chisel语言.02.Chisel+VS Code+Ubuntu/WSL2开发环境搭建与使用

既然是学Chisel语言,那肯定得先把环境配置好,磨刀不误砍柴工嘛。而且有个测试环境的话,在后期的学习过程中自己去试运行代码段可以加深学习效果。这里推荐使用Ubuntu或者基于WSL2(Windows Subsystem Linux 2)的Ubuntu,本人使用的是WSL2上的Ubuntu 20.04.3 LTS,在使用体验上与Ubuntu几乎没有区别,且能同时使用Windows提供的诸多便利。...

github-3rr0r的博客 8449

【原创】关于Adapter的The content of the adapter has changed问题分析

关于Adapter的The content of the adapter has changed问题分析 1、问题描述 1 07-28 17:22:02.162: E/AndroidRuntime(16779): java.lang.IllegalStateException: The content of the adapter has changed but Lis...

weixin_30481087的博客 193

从XSA到Linux应用:Petalinux与Vitis的嵌入式开发全流程解析

本文详细解析了从XSA文件到Linux应用的嵌入式开发全流程,重点介绍了Petalinux与Vitis工具链的使用技巧和实战经验。通过XSA硬件描述、Petalinux工程搭建、系统镜像优化及Vitis开发配置等关键步骤,帮助开发者高效完成ZYNQ平台开发,解决常见问题并提升开发效率。

weixin_29195217的博客 192

FastQC使用与结果详细解读

FastQC的使用与结果详细解读

win_win223的博客 1万+

银河麒麟V10系统grub崩溃急救指南:从光盘救援到完整恢复(附常见错误排查)

本文详细介绍了银河麒麟V10系统grub崩溃的急救方法,从使用安装光盘进入救援环境到完整恢复系统的全流程。针对不同损坏程度(轻度、中度、严重)提供了分级恢复策略,包括grub2-install和grub2-mkconfig等关键命令的使用,并附有常见错误排查表,帮助用户快速解决引导问题。

weixin_30955617的博客 484

使用vcpkg快速安装opencascade并自动配置部署开发环境

广泛的开源库:vcpkg 提供了上千个流行的 C++ 开源库,用户可以通过简单的命令安装和集成这些库,包括 Boost、OpenCV、SQLite、fmt 等。确保包版本一致性:通过 vcpkg.json 文件,开发者可以指定依赖的库版本,确保项目在不同环境下的构建结果一致,避免由于依赖库版本不一致而导致的构建问题。强大的社区支持:Vcpkg 是开源项目,拥有活跃的社区和微软团队的支持,提供了稳定的更新和问题修复。快速更新:Vcpkg 社区活跃,库的更新速度较快,开发者可以迅速获得库的最新版本。

loujiand的博客 2295

Adapter报错:The content of the adapter has changed

Adapter报错:The content of the adapter has changed

Working harder, getting stronger! 1万+

不止于HTTPS请求:用VS2022编译的libcurl玩转文件上传和代理设置

本文详细介绍了如何使用VS2022编译的libcurl库实现文件上传和高级代理配置。通过实战代码示例,展示了多部分表单上传、二进制数据上传以及各种代理设置技巧,帮助C++开发者提升网络编程能力。

weixin_28595749的博客 206

Android适配器更改,ListView未收到通知解析

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.

夨坕的博客 637

通用定时器脉冲计数实验(STM32CubeMx配置)

通用定时器脉冲计数实验

a1169511636的博客 5510

手势识别器:实现手部动作识别器

在本篇文章中,我们将使用Python来实现一个简单的手部动作识别器。然后,我们通过循环从摄像头获取每一帧图像,并对每一帧图像进行处理。我们提取手部关键点并计算手势的特征向量,然后将其添加到特征列表和标签列表中。接下来,我们再次从摄像头获取每一帧图像,并进行手势识别。我们提取手部关键点并计算手势的特征向量,然后使用训练好的分类器来预测手势,并在图像上显示出来。在这个实现中,我们将使用MediaPipe库来检测和跟踪手部关键点,然后使用K最近邻(K-Nearest Neighbors)算法对手势进行分类。

BitSlinger的博客 241

The content of the adapter has changed but ListView did not receive a notification.

在Android编程中使用Adapter时,【偶尔】会出现如下错误: 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, ...

weixin_30950237的博客 4450

【Unity 实用工具篇】 | Unity网格破碎插件 unity-fracture

前面写过一篇Unity中物体破碎效果的插件:【Unity 实用工具篇】 | Unity物体破碎效果 OpenFracture 本文介绍一个另外的工具unity-fracture,也可以实现网格破碎效果,且功能与OpenFracture略有差异。

努力前行,总会成为自己心中的那道光 1万+

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. M...

amos1993的博客 282

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

java.lang.IllegalStateException:The content of the adapter has changed but ListView did not receiv问题

在使用ListView过程中,有时会出现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 th...

CrackgmKey的博客 1276

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

如何在安卓上优雅的搜索文件,解决The content of the adapter has changed but ListView did not receive a notification报错

码字辛苦!转载请注明出处! 我们知道,在安卓上运行JAVA有严格的内存限制,不能像PC上那样肆无忌惮的使用递归和多线程,否则~ OOM,经典不~ 那么,如何在安卓上优雅搜索文件呢? 关键点就在于: 1、取代递归 2、适时终止上一个搜索线程 3、在主线程中更新List和View 4、避免线程中Activity对象造成的内存泄漏 ——那我们把需要搜索的文件目录记录下来循环遍历...

不会前端的安卓不是好JAVA工程师 340

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

The content of the adapter has changed but ListView did not receive a notification

转载:http://blog.youkuaiyun.com/garybook/article/details/7498342 The content of the adapter has changed but ListView did not receive a notification 在Android开发过程中,使用了大量的ListView,发现这个错误偶尔会出现。特别是做压力测试的时候,不停的点击

fangjingjingll的专栏 374

解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题

解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题 标签: androidadapterUI thread 2014-03-06 09:47 23888人阅读 评论(10) 收藏 举报  分类: Android(18) 

anroidyanyou的博客 539

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive...

ListView UI重绘时触发layoutChildren, 此时会校验listView的mItemCount与其Adapter.getCount是否相同,不同报错。 ListView.layoutChildren: mItemCount是在父类AdapterView中定义的,package类型 在两个地方mItemCount会被赋值,初始设置Adapter时: xxAda...

weixin_30323961的博客 116

问题解决:The content of the adapter has changed but ListView did not receive a notification

1. 不要在后台线程中直接调用adapter 2. 不要在后台线程中修改adapter绑定的数据 如果对adapter或者adapter绑定的数据是在线程中,加上runOnUiThread就可以了 runOnUiThread(new Runnable(){ public void run(){ //修改数据 d...

weixin_30512043的博客 87

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification String keyStr = msg.obj.toString(); //在改变listview的数据后一定要...

weixin_30906671的博客 149

解决java.lang.IllegalStateException: The application’s PagerAdapter changed the adapter’s content...

A界面中有viewpager的动态加载,从界面A跳到界面B,再finish掉B返回A时报出此异常。 java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChang...

djh10000的博客 839

Android报错:The content of the adapter has changed...与Channel is unrecoverably broken and will be disp...

在Android中adapter错误: 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 t...

weixin_30823833的博客 173

android The content of the adapter has changed but ListView did not receive a notification 错误的解决方案...

使用了AsyncTask在后台刷新适配器,并且通知ui线程更新ListView,运行时发现时不时的出现 如题 的错误, 导致程序崩溃,解决方法如下: 1.建立一个缓冲数据集,这个数据集就是填充适配器的数据集,如ArrayList<String> 2.在AsyncTask的后台方法(doInBackground)中将数据添加至缓冲数据集中,然后调用onProgressUpdate通...

weixin_30670925的博客 131

The content of the adapter has changed but ListView did not receive a notification. Make sure the co...

我出现这个问题是引用资源文件问题helper.getView(R.id.in_pic).setBackgroundResource(item.getResourceId()); //错的helper.getView(R.id.in_pic).setBackground(getResources().getDrawable(item.getResourceId()));//对的 转载于:http...

weixin_30483495的博客 82
上一篇: Android事件分发总结
下一篇: 关于synchronized 关键字
AndroidGuy
博客等级 码龄15年 4粉丝 13原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值