转自:RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案

RecyclerView是Android-support-v7-21版本中新增的一个Widget,RecyclerView在刷新数据的时候会出现以下异常:

  1. java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{431a7450 position=1 id=-1, oldPos=-1, pLpos:-1 scrap [attachedScrap] tmpDetached no parent}
  2. at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4251)
  3. at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4382)
  4. at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
  5. at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
  6. at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
  7. at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
  8. at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
  9. at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
  10. at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
  11. at android.view.View.layout(View.java:14828)
  12. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  13. at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:584)
  14. at android.view.View.layout(View.java:14828)
  15. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  16. at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
  17. at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
  18. at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
  19. at android.view.View.layout(View.java:14828)
  20. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  21. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
  22. at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
  23. at android.view.View.layout(View.java:14828)
  24. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  25. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
  26. at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
  27. at android.view.View.layout(View.java:14828)
  28. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  29. at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
  30. at android.view.View.layout(View.java:14828)
  31. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  32. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
  33. at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
  34. at android.view.View.layout(View.java:14828)
  35. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  36. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
  37. at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
  38. at android.view.View.layout(View.java:14828)
  39. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  40. at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
  41. at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
  42. at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
  43. at android.view.View.layout(View.java:14828)
  44. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  45. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
  46. at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
  47. at android.view.View.layout(View.java:14828)
  48. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  49. at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
  50. at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
  51. at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
  52. at android.view.View.layout(View.java:14828)
  53. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  54. at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
  55. at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
  56. at android.view.View.layout(View.java:14828)
  57. at android.view.ViewGroup.layout(ViewGroup.java:4631)
  58. at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2074)
  59. at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1831)
  60. at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1087)
  61. at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5696)
  62. at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
  63. at android.view.Choreographer.doCallbacks(Choreographer.java:574)
  64. at android.view.Choreographer.doFrame(Choreographer.java:544)
  65. at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
  66. at android.os.Handler.handleCallback(Handler.java:733)
  67. at android.os.Handler.dispatchMessage(Handler.java:95)
  68. at android.os.Looper.loop(Looper.java:136)
  69. at android.app.ActivityThread.main(ActivityThread.java:5016)
  70. at java.lang.reflect.Method.invokeNative(Native Method)
  71. at java.lang.reflect.Method.invoke(Method.java:515)
  72. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
  73. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
  74. 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上找到了一个解决方法,链接如下:

http://stackoverflow.com/questions/31759171/recyclerview-and-java-lang-indexoutofboundsexception-inconsistency-detected-in

其实也不是什么解决方法,只是把这个异常捕获了,不让他奔溃了,这个问题的终极解决方案还是得让google去修复。

1、创建一个类LinearLayoutManagerWrapper继承LinearLayoutManager,重写onLayoutChildren方法

  1. public class WrapContentLinearLayoutManager extends LinearLayoutManager {
  2. public WrapContentLinearLayoutManager(Context context) {
  3. super(context);
  4. }
  5. public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
  6. super(context, orientation, reverseLayout);
  7. }
  8. public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  9. super(context, attrs, defStyleAttr, defStyleRes);
  10. }
  11. @Override
  12. public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
  13. try {
  14. super.onLayoutChildren(recycler, state);
  15. } catch (IndexOutOfBoundsException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }

2、设置RecyclerView的布局管理为WrapContentLinearLayoutManager对象

  1. mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

我经过大量的测试,发现这个异常真的被catch了,没有再出现这种崩溃的问题了,大功告成,希望对大家有所帮助!!!

RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)的更多相关文章

  1. RecycleView错误: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder

    1.错误 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positi ...

  2. 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,在数据更新时会出 ...

  3. 滑动RecyclerView时出现异常: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:30

    RecyclerView 存在的一个明显的 bug 一直没有修复: java.lang.IndexOutOfBoundsException: Inconsistency detected. Inval ...

  4. Recyclerview 出现 java.lang.IndexOutOfBoundsException: Inconsistency detected 异常

    使用 RecyclerView 的时候报错 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view hold ...

  5. RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.

    今天使用RecyclerView时,上下两个RecyclerView,在实现下拉刷新时,报错: java.lang.IndexOutOfBoundsException: Inconsistency d ...

  6. android view holder 优化

    android 一般都用viewholder来优化contentView,采用sparseArray能够进一步优化 /** * 用法: ImageView bananaView = ViewHolde ...

  7. 编译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驱 ...

  8. 解析RecyclerView(2)——带顶部View和底部View的RecyclerView

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  9. 解决tensorflow 调用bug Running model failed:Invalid argument: NodeDef mentions attr 'dilations' not in Op<name=Conv2D; signature=input:T, filter:T ->

    将tensorflow C++ 版本更新为何训练版本一致即可

随机推荐

  1. jQuery滑动方法

    jQuery 滑动方法 通过 jQuery,您可以在元素上创建滑动效果. jQuery 拥有以下滑动方法: slideDown() slideUp() slideToggle() jQuery sli ...

  2. 个人博客 V0.0.3 版本 ...

    早就想弄个人博客网站,一直拖到现在...事情总是忙不完的,想能力提升的快,只能挤时间多练多写了,Keep On Unsleeping 以后原创的笔记都会在自己的博客网站写,博客园和简书就用来转发文章了 ...

  3. ubuntu配置无密码登录

    1 本地生成ssh公钥和私钥, 2将公钥拷贝到ubuntu上的.ssh/authorized_keys 中

  4. squid.con 配置文件详解

    博客转载:http://www.articleswriting.net/article/6477447043/;jsessionid=42C9702B475ECF99EB861214186390E8 ...

  5. ylbtech_dbs_article_五大主流数据库模型

    ylbtech_dbs_article 摘要:什么是数据模型? 访问数据库中的数据取决于数据库实现的数据模型.数据模型会影响客户端通过API对数据的操作.不同的数据模型可能会提供或多或少的功能.一般而 ...

  6. “/”和“\\”和feof();

    filename=c:/test/abc.text filename=c:\\test\\abc.test "\\"为转义字符: feof();函数检测文件是否已经到达末尾(EOF ...

  7. wildfly配置PostgreSQL数据源

    wildfly配置PostgreSQL数据源 很久以前写过在JBoss AS7上如何配置PostgreSQL数据源,此方法在wildfly上依然有效.不过,通过wildfly的命令行工具可以有更简单的 ...

  8. 在eclipse中使用Lombok

    1.下载Lombok.jar http://projectlombok.googlecode.com/files/lombok.jar2.运行Lombok.jar: java -jar  D:\001 ...

  9. ElasticSearch 分布式集群

    1.前言 Elasticsearch用于构建高可用和可扩展的系统.扩展的方式可以是购买更好的服务器(纵向扩展(vertical scale or scaling up))或者购买更多的服务器(横向扩展 ...

  10. Poj 4227 反正切函数的应用

    Description 反正切函数可展开成无穷级数,有例如以下公式 (当中0 <= x <= 1) 公式(1) 使用反正切函数计算PI是一种经常使用的方法.比如,最简单的计算PI的方法: ...