在项目中遇到一个RecyclerView 偶现的奔溃,查看日志,发现是:

java.lang.IndexOutOfBoundsException: Index: 39, Size: 39

at java.util.LinkedList.checkElementIndex(LinkedList.java:555)

at java.util.LinkedList.get(LinkedList.java:476)

at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:4844)

at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3909)

at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3540)

at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)

at android.view.View.layout(View.java:19781)

at android.view.ViewGroup.layout(ViewGroup.java:6144)

at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:606)

at android.view.View.layout(View.java:19781)

at android.view.ViewGroup.layout(ViewGroup.java:6144)

at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)

at android.widget.FrameLayout.onLayout(FrameLayout.java:261)

at android.view.View.layout(View.java:19781)

at android.view.ViewGroup.layout(ViewGroup.java:6144)

at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1816)

at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1660)

at android.widget.LinearLayout.onLayout(LinearLayout.java:1569)

at android.view.View.layout(View.java:19781)

猜测原因是,RecyclerView的对于的数据被更改,比如,RecyclerView 当前的List 的size是39,然后,把这个List 删除了一部分,这个操作没有及时调用RecyclerView的notify造成了。

由于项目这个List修改的地方很多,很底层,目前使用临时的解决方案(网上其他方案都无法彻底catch这个异常,有这个烦恼的,请查看下面的代码),对异常进行try-catch 处理,具体代码如下:

public class LinearLayoutMgr extends LinearLayoutManager {
public LinearLayoutMgr(Context context) {
super(context);
} public LinearLayoutMgr(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
} public LinearLayoutMgr(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 (Exception e) {
e.printStackTrace();
LogUtil.d("catch exception");
}
} @Override
public void scrollToPosition(int position) {
try {
super.scrollToPosition(position);
} catch (Exception e) {
e.printStackTrace();
LogUtil.d("catch exception");
}
}
}

在初始化RecyclerView 的时候,使用这个LinearLayoutMgr 代替系统的LinearLayoutManager即可

RecyclerView IndexOutOfBoundsException 问题的更多相关文章

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

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

  2. Android中RecyclerView出现java.lang.IndexOutOfBoundsException

    在RecyclerView更细数据时出现java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder ...

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

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

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

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

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

  6. RecyclerView解密篇(三)

    在上一篇(RecyclerView使用详解(二))文章中介绍了RecyclerView的多Item布局实现,接下来要来讲讲RecyclerView的Cursor实现,相较于之前的实现,Cursor有更 ...

  7. RecyclerView 介绍 02 – 重要概念

    几个概念 RecyclerView是一个ViewGroup: LayoutManager控制RecyclerView的ChildView的布局显示,childview由Recycler提供以及管理: ...

  8. RecyclerView和ScrollView嵌套使用

    我们的recyclerView有多个layoutmanager,通过重写layoutmanager的方法就可以让recyclerView和ScrollView嵌套了.但是请注意,如果recyclerV ...

  9. Android RecyclerView使用详解(三)

    在上一篇(RecyclerView使用详解(二))文章中介绍了RecyclerView的多Item布局实现,接下来要来讲讲RecyclerView的Cursor实现,相较于之前的实现,Cursor有更 ...

随机推荐

  1. [ActionScript 3.0] 如何获得实例对象的类名及类

    package { import flash.display.DisplayObject; import flash.display.MovieClip; import flash.display.S ...

  2. Ionic2文档整理

    来自:Rainey's Blog 原文地址:http://rainey.space/2016/04/06/Ionic2_Chinese_Document/ Github:https://github. ...

  3. c语言-折半查找的函数

    void search(int n,int num[],char name[N][10]) { int top,bottom,middle,location,flag; top=0; bottom=N ...

  4. sqlalchemy中使用event设置条件触发短信与邮件通知

    一.原因 近期在做短信与邮件通知系统.使用到了这一块.例如,当订单完成以后进行邮件短信的通知.虽然可以采用直接调用接口的方式实现,但有几个原因让我希望使用条件触发的方式 1.由于系统中支持线上线下以及 ...

  5. [CoffeeScript]使用Yield功能

    CoffeeScript 1.9 开始提供了类似ES6的yield关键字. 自己结合co和bluebird做了个试验. co -- http://npmjs.org/package/co  -- fo ...

  6. Rstudio所有快捷键 “原版+中文” 整理

  7. Tr A(矩阵快速幂)

    A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据.  每组数据的第一行有n(2 <= n < ...

  8. odoo 的字段。orm对象

    OpenERP ORM 对象方法列表   OpenERP对象支持的字段类型有,基础类型:char, text, boolean, integer, float, date, time, datetim ...

  9. 解决VS在高DPI下设计出的Winform程序界面变形问题

    在目前高分屏流行的情况下,windows缩放与布局仍然设置为100%就显得太小(特别是笔记本),通常会调整为125%或150%, VS在缩放与布局设置为非100%的时候,就会自动启动DPI感知模式,以 ...

  10. es第三篇:Search APIs

    大多数search API都是可以操作多个索引的,除了explain API. 当执行一个search API时,可以指定routing参数,去搜索特定的主分片及其副本分片.routing参数值可以是 ...