RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)
转自:RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案
RecyclerView是Android-support-v7-21版本中新增的一个Widget,RecyclerView在刷新数据的时候会出现以下异常:
- java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{431a7450 position=1 id=-1, oldPos=-1, pLpos:-1 scrap [attachedScrap] tmpDetached no parent}
- at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4251)
- at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4382)
- at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
- at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
- at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
- at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
- at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
- at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
- at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:584)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
- at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
- at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
- at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
- at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
- at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
- at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2074)
- at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1831)
- at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1087)
- at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5696)
- at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
- at android.view.Choreographer.doCallbacks(Choreographer.java:574)
- at android.view.Choreographer.doFrame(Choreographer.java:544)
- at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
- at android.os.Handler.handleCallback(Handler.java:733)
- at android.os.Handler.dispatchMessage(Handler.java:95)
- at android.os.Looper.loop(Looper.java:136)
- at android.app.ActivityThread.main(ActivityThread.java:5016)
- at java.lang.reflect.Method.invokeNative(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:515)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
- at dalvik.system.NativeStart.main(Native Method)
大家看这个代码,只是并没有报到我们自己的代码里面来,在底层就崩溃了,在app层面并没有,弹出一个框,说应用程序已奔溃,而是直接就没了,用户感觉很奇怪。这种异常并不是很容易出现,而是偶尔出现,我的也是在后台奔溃日志中,发现了这种异常,我们自己都不知道什么地方报错的,搜索百度,发现有个链接的权重比较高,看的人也比较多:
http://drakeet.me/recyclerview-bug-indexoutofboundsexception-inconsistency-detected-invalid-item-position-solution?utm_source=tuicool&utm_medium=referral
但是,这篇文章写得比较模糊,解决方案也只是给出一个代码片段,我都不知道怎么加进去,之后我用google去搜索,发现这个问题很普遍,很多都是偶尔出现,很难去重现他,终于在stackoverflow上找到了一个解决方法,链接如下:
其实也不是什么解决方法,只是把这个异常捕获了,不让他奔溃了,这个问题的终极解决方案还是得让google去修复。
1、创建一个类LinearLayoutManagerWrapper继承LinearLayoutManager,重写onLayoutChildren方法
- public class WrapContentLinearLayoutManager extends LinearLayoutManager {
- public WrapContentLinearLayoutManager(Context context) {
- super(context);
- }
- public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
- super(context, orientation, reverseLayout);
- }
- public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- }
- @Override
- public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
- try {
- super.onLayoutChildren(recycler, state);
- } catch (IndexOutOfBoundsException e) {
- e.printStackTrace();
- }
- }
- }
2、设置RecyclerView的布局管理为WrapContentLinearLayoutManager对象
- mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
我经过大量的测试,发现这个异常真的被catch了,没有再出现这种崩溃的问题了,大功告成,希望对大家有所帮助!!!
RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)的更多相关文章
- RecycleView错误: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder
1.错误 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positi ...
- Android之RecyclerView的原生Bug-Inconsistency detected. Invalid view holder adapter positionViewHolder{a1bbfa3 position=2 id=-1, oldPos=-1, pLpos:-1 no parent}
今天在运行自己编写的App时,突然发现App在运行时闪退,然后就查看了Android Studio的Log,发现了这个错误,上网查了一下,才知道是RecyclerView的原生Bug,在数据更新时会出 ...
- 滑动RecyclerView时出现异常: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:30
RecyclerView 存在的一个明显的 bug 一直没有修复: java.lang.IndexOutOfBoundsException: Inconsistency detected. Inval ...
- Recyclerview 出现 java.lang.IndexOutOfBoundsException: Inconsistency detected 异常
使用 RecyclerView 的时候报错 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view hold ...
- RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.
今天使用RecyclerView时,上下两个RecyclerView,在实现下拉刷新时,报错: java.lang.IndexOutOfBoundsException: Inconsistency d ...
- android view holder 优化
android 一般都用viewholder来优化contentView,采用sparseArray能够进一步优化 /** * 用法: ImageView bananaView = ViewHolde ...
- 编译OpenGL代码时发生 Inconsistency detected by ld.so: dl-version.c: 224: _dl_check_map_versions: Assertion `needed != ((void *)0)' failed! 错误的解决方案
注:本解决方案适用于使用N卡的PC 出现该错误 , 一般是由于开源的nouveau驱动和Nvidia专有驱动冲突导致的 .在解决该问题时 , 尝试过卸载 N 卡专有驱动 , 仅使用开源nouveau驱 ...
- 解析RecyclerView(2)——带顶部View和底部View的RecyclerView
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- 解决tensorflow 调用bug Running model failed:Invalid argument: NodeDef mentions attr 'dilations' not in Op<name=Conv2D; signature=input:T, filter:T ->
将tensorflow C++ 版本更新为何训练版本一致即可
随机推荐
- jQuery滑动方法
jQuery 滑动方法 通过 jQuery,您可以在元素上创建滑动效果. jQuery 拥有以下滑动方法: slideDown() slideUp() slideToggle() jQuery sli ...
- 个人博客 V0.0.3 版本 ...
早就想弄个人博客网站,一直拖到现在...事情总是忙不完的,想能力提升的快,只能挤时间多练多写了,Keep On Unsleeping 以后原创的笔记都会在自己的博客网站写,博客园和简书就用来转发文章了 ...
- ubuntu配置无密码登录
1 本地生成ssh公钥和私钥, 2将公钥拷贝到ubuntu上的.ssh/authorized_keys 中
- squid.con 配置文件详解
博客转载:http://www.articleswriting.net/article/6477447043/;jsessionid=42C9702B475ECF99EB861214186390E8 ...
- ylbtech_dbs_article_五大主流数据库模型
ylbtech_dbs_article 摘要:什么是数据模型? 访问数据库中的数据取决于数据库实现的数据模型.数据模型会影响客户端通过API对数据的操作.不同的数据模型可能会提供或多或少的功能.一般而 ...
- “/”和“\\”和feof();
filename=c:/test/abc.text filename=c:\\test\\abc.test "\\"为转义字符: feof();函数检测文件是否已经到达末尾(EOF ...
- wildfly配置PostgreSQL数据源
wildfly配置PostgreSQL数据源 很久以前写过在JBoss AS7上如何配置PostgreSQL数据源,此方法在wildfly上依然有效.不过,通过wildfly的命令行工具可以有更简单的 ...
- 在eclipse中使用Lombok
1.下载Lombok.jar http://projectlombok.googlecode.com/files/lombok.jar2.运行Lombok.jar: java -jar D:\001 ...
- ElasticSearch 分布式集群
1.前言 Elasticsearch用于构建高可用和可扩展的系统.扩展的方式可以是购买更好的服务器(纵向扩展(vertical scale or scaling up))或者购买更多的服务器(横向扩展 ...
- Poj 4227 反正切函数的应用
Description 反正切函数可展开成无穷级数,有例如以下公式 (当中0 <= x <= 1) 公式(1) 使用反正切函数计算PI是一种经常使用的方法.比如,最简单的计算PI的方法: ...