RecyclerView IndexOutOfBoundsException 问题
在项目中遇到一个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 问题的更多相关文章
- 滑动RecyclerView时出现异常: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:30
RecyclerView 存在的一个明显的 bug 一直没有修复: java.lang.IndexOutOfBoundsException: Inconsistency detected. Inval ...
- Android中RecyclerView出现java.lang.IndexOutOfBoundsException
在RecyclerView更细数据时出现java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder ...
- Recyclerview 出现 java.lang.IndexOutOfBoundsException: Inconsistency detected 异常
使用 RecyclerView 的时候报错 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view hold ...
- RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)
转自:RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解 ...
- 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解密篇(三)
在上一篇(RecyclerView使用详解(二))文章中介绍了RecyclerView的多Item布局实现,接下来要来讲讲RecyclerView的Cursor实现,相较于之前的实现,Cursor有更 ...
- RecyclerView 介绍 02 – 重要概念
几个概念 RecyclerView是一个ViewGroup: LayoutManager控制RecyclerView的ChildView的布局显示,childview由Recycler提供以及管理: ...
- RecyclerView和ScrollView嵌套使用
我们的recyclerView有多个layoutmanager,通过重写layoutmanager的方法就可以让recyclerView和ScrollView嵌套了.但是请注意,如果recyclerV ...
- Android RecyclerView使用详解(三)
在上一篇(RecyclerView使用详解(二))文章中介绍了RecyclerView的多Item布局实现,接下来要来讲讲RecyclerView的Cursor实现,相较于之前的实现,Cursor有更 ...
随机推荐
- mysqldump导出数据不带时区信息的问题
今天在导出数据时,发现所有timestamp字段都不带时区信息,因为我在东8区,导出的数据中所有时间都提早了8个小时 首先先看表的字段和数据 CREATE TABLE IF NOT EXISTS `a ...
- short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
对于 short s1 = 1; s1 = s1 + 1;由于 s1+1运算时会自动提升表达式的类型,所以结果是 int型,再赋值给 short 类型 s1时, 编译器将报告需要强制转换类型的错误.对 ...
- Layout2:StackPanel(补交作业)
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" > <Rect ...
- HTML-利用CSS和JavaScript制作一个切换图片的网页
由于个人原因,不详细写步骤 思路: 一.布局 二.制作图片区和按钮区的div及颜色.边框.背景属性等 三.用PS将四张图片剪切到同一个尺寸,重叠放置在图片切换区,透明度设置为0 四.点击对应按钮时,将 ...
- BZOJ1688 Disease Manangement 疾病管理
Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D ...
- 一些排序 (python实现)
►快速排序 <时间复杂度O(n㏒n)> def partition(li,left,right): tmp = li[left] while left < right: while ...
- PHP错误与异常处理
https://www.cnblogs.com/zyf-zhaoyafei/p/6928149.html 请一定要注意,没有特殊说明:本例 PHP Version < 7 说起PHP异常处理,大 ...
- Nginx unknown directive ""
原因:由于使用记事本编辑了nginx.conf. 解决方案:参考https://www.jianshu.com/p/2516ec8bae72
- js中this那些事儿
前几天写东西由于恶趣味作祟将所有的函数全部封装在json中,起初好好的,函数B也可以调用函数A的内容,不过在写一个点击事件时出现了意外, 代码如下: var $ ={ "A":fu ...
- springboot+自定义注解实现灵活的切面配置
利用aop我们可以实现业务代码与系统级服务例如日志记录.事务及安全相关业务的解耦,使我们的业务代码更加干净整洁. 最近在做数据权限方面的东西,考虑使用切面对用户访问进行拦截,进而确认用户是否对当前数据 ...