在项目中遇到一个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. nginx高性能WEB服务器系列之九--nginx运维故障日常解决方案

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  2. Navicat设定mysql定时任务

    有个需求:每天将一张表的前一天的数据抽取到另一张表中,使用Mysql数据库的客户端Navicat配置 第一步,创建过程cust_report,直接在查询窗口中执行,保存后函数列表中就会出现. 第二步, ...

  3. vue-cli 3.5 解决 typescript cannot find file 问题。

    版本: "ts-loader": "^3.5.0","typescript": "^3.3.4000", "v ...

  4. 牛客 Wannafly挑战赛27 D 绿魔法师

    传送门 \(\color{green}{solution}\) 分析下,在\(1e5+1\)内,一个数的约数个数最多为\(2^{6}\)个,所以我们可以考虑枚举约数 复杂度\(O(N^{2^{6 \t ...

  5. ABP与多租户

    ABP简介 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应用程 ...

  6. OAuth2 .net MVC实现获取token

    OAuth2 的原理这里不多讲,可以看:https://www.cnblogs.com/icebutterfly/p/8066548.html 直奔主题:这里要实现的功能为,统计微软的Owin程序集实 ...

  7. mysql 5.7 的 /etc/my.cnf

    mysql的my.cnf文件纯净版的.随意享用. # Example MySQL config file for medium systems. # # This is for a system wi ...

  8. goledengate重新投递和目标端跳过过事务

    日常在goledengate的维护中,最大的问题莫过于进程ABENDING.在我的维护生涯中,主要的有两个原因,第一个是网络中断造成的造成的文件损坏,一个是大事务(相关操作人员在进行操作的时候事务过大 ...

  9. oracle表空间tablespace

    用户:用户是在实例下建立的.不同实例可以建相同名字的用户. 表空间:表空间是一个用来管理数据存储逻辑概念,表空间只是和数据文件(ORA或者DBF文件)发生关系,数据文件是物理的,一个表空间可以包含多个 ...

  10. Part 1: Setting up ARM GNU tool chain

    ARM Build Tools GNU Tools for ARM Embedded Processors. GNU Make for Windows. GNU Tools for ARM Embed ...