解决java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionV
解决java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder
bug日志:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{ecef223 position=46 id=-1, oldPos=-1, pLpos:-1 no parent} android.support.v7.widget.RecyclerView{9876d9c VFED… … 0,0-1052,1651 #7f100210 app:id/recyclerView}, adapter:net.liexiang.dianjing.adapter.AdapterFeatureListRv@4b763a5, layout:android.support.v7.widget.LinearLayoutManager@90fe37a, context:net.liexiang.dianjing.ui.order.feature.FeatureListActivity@95fa84
at android.support.v7.widget.RecyclerViewRecycler.validateViewHolderForOffsetPosition(RecyclerView.java:5610)atandroid.support.v7.widget.RecyclerViewRecycler.validateViewHolderForOffsetPosition(RecyclerView.java:5610) at android.support.v7.widget.RecyclerViewRecycler.validateViewHolderForOffsetPosition(RecyclerView.java:5610)atandroid.support.v7.widget.RecyclerViewRecycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5792)
at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:285)
at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:342)
at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:358)
at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:365)
at android.support.v7.widget.GapWorker.run(GapWorker.java:396)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
原因:
通过查阅官方论坛和网上资料,基本锁定为RecyclerView官方的BUG
解决方式:
方式一:
继承封装LinearLayoutManager类,重写onLayoutChildren()方法,try-catch捕获该异常。
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
/**
Created by fx on 2019/9/11.
*/
public class XLinearLayoutManager extends LinearLayoutManager {public XLinearLayoutManager(Context context) {
super(context);
}public XLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}public XLinearLayoutManager(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) {
Log.e(“bug”, “crash in RecyclerView”);
}
}
}
LinearLayoutManager mLayoutManager=new XLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(mLayoutManager);
方式二:
数据变动(移除和增加),执行同步操作时,要保证RecyclerView的Adapter中的数据与需要同步的外部集合数据数量一致!
即每一次外部数据变动(移除和增加)时,都需主动Adapter做一次数据同步操作。以避免数据同步时内外数据不一致
数据同步方法:
notifyItemRangeRemoved();
notifyItemRangeInserted();
notifyItemRangeChanged();
notifyDataSetChanged();
异常代码:
public void setData(List list) {
if (list != null ) {
array.clear();
array.addAll(list);
notifyItemRangeChanged(0, list.size());
}
}
这里对该外部数据做了两个操作:先移除数据,然后添加数据,之后notify数据集。
array.addAll(list)时,Adapter的内部数据还处在array.clear()之前的状态,造成了Adapter内外数据不一致。
修复代码:
public void setData(List list) {
if (list != null ) {
int size = array.size();
array.clear();
notifyItemRangeRemoved(0, size);
array.addAll(list);
notifyItemRangeChanged(0, list.size());
}
}
我是用的是第一种方法,只要考虑到第二种方法在数据频繁变动时需要频繁进行同步操作。
解决java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionV相关推荐
- RecyclerView问题:java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adap
###写在前面 在使用RecyclerView中出现了一个问题,如下图 ###解决办法: 我们在使用RecycxlerView时,通常先回设置一个LayoutManager 我们的问题出现在Layou ...
- 解决IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter
解决IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter 最近项目已个要求,recyclervi ...
- Inconsistency detected Invalid view holder adapter position
RecyclerView在添加数据的时候发生了异常. java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view ...
- Inconsistency detected. Invalid view holder adapter positionVH
recycleview报错,是因为我的list引用了静态变量的list,直接=了,应该是list.addAll(静态变量list)
- Recyclerview 出现 java.lang.IndexOutOfBoundsException: Inconsistency detected 异常
使用 RecyclerView 的时候报错 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view hold ...
- java.lang.IndexOutOfBoundsExceptionInconsistency detected. Invalid view holder adapter position问题处理
工作中发现bugly上报了一个崩溃,日志如下: java.lang.IndexOutOfBoundsExceptionInconsistency detected. Invalid view hold ...
- IDEA报错Error:Module 'shop-common' production: java.lang.IndexOutOfBoundsException
问题描述: 本来项目是正常的,编译.运行.启动都是OK的,但是在一次电脑重启后,出现了以上这个问题:Error:Module 'shop-common' production: java.lang.I ...
- 报错,null [java.lang.IndexOutOfBoundsException,Index: 5, Size: 5]
问题: { "success": false, "code": "S0000", "error" ...
- 解决“java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM”错误
1.参考:解决"java.lang.OutOfMemoryError: Failed to allocate a allocation until OOM"错误_SEVENY_的博 ...
- java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
哎 这类问题 本来都不打算记录的,但是自己写的时候还是犯了错误,一运行报错了 大致就是list 超出了 ,可能index size 值不一样( Index: 9, Size: 9, Index: 5, ...
最新文章
- 微信开放平台手机APP支付
- SpringBoot--Druid多数据源配置
- 创建python2与python3虚拟环境失败的原因
- html相对路径载入页面,html页面的绝对路径和相对路径
- LVS(9)——为NAT单独增加路由器
- 【翻译】GitHub Pages Basics 基本使用帮助【一】GitHub Pages 是什么?
- CALayer与UIBezierPath
- 图书管理系统的5W1H
- 检讨:丢了我女儿和项目设计感想
- CGAL 4.9 - Triangulated Surface Mesh Deformation
- Shell 工作原理
- 如何在svn上新建一个目录并赋予相应的权限
- VISTA组策略中关闭自动播放的位置
- 豆瓣电影爬虫Ⅱ 豆瓣电影数据的整理
- 华为计算机网络技术试题,(完整版)华为试题答案
- 【谷粒商城】k8s、devops集群篇(4/4)
- Java可以加速播放的播放器,基于live555的rtsp播放器之十二:使用soundtouch加速音频播放...
- Python爬取网易云音乐评论
- 简单欧拉公式c语言算法,算法之_欧拉公式
- asp.net中调用javascript函数实现多功能日期控件示例
热门文章
- 怎么用C++实现点对点通信
- GEE系列:第 1 单元 Google 地球引擎简介
- win10计算机还原点如何创建,win10系统创建还原点及系统还原的操作方法
- Kubernetes Secrets
- 通过存储控制器访问外设
- quantization 顶会文章简介 2017
- 《一个人的朝圣》的读后感作文3100字
- Win10下AutoCAD2014 +VS2010 + Object2014 安装与配置环境
- 手机卫星定位系统_北斗三号最后一颗卫星发射成功完成组网!为何手机上还显示GPS?...
- ffmpeg合并多mp4视频