RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)
转自:RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案
RecyclerView是Android-support-v7-21版本中新增的一个Widget,RecyclerView在刷新数据的时候会出现以下异常:
- java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{431a7450 position=1 id=-1, oldPos=-1, pLpos:-1 scrap [attachedScrap] tmpDetached no parent}
- at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4251)
- at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4382)
- at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
- at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
- at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
- at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
- at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
- at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
- at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:584)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
- at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
- at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
- at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
- at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
- at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
- at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
- at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
- at android.view.View.layout(View.java:14828)
- at android.view.ViewGroup.layout(ViewGroup.java:4631)
- at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2074)
- at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1831)
- at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1087)
- at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5696)
- at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
- at android.view.Choreographer.doCallbacks(Choreographer.java:574)
- at android.view.Choreographer.doFrame(Choreographer.java:544)
- at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
- at android.os.Handler.handleCallback(Handler.java:733)
- at android.os.Handler.dispatchMessage(Handler.java:95)
- at android.os.Looper.loop(Looper.java:136)
- at android.app.ActivityThread.main(ActivityThread.java:5016)
- at java.lang.reflect.Method.invokeNative(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:515)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
- 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上找到了一个解决方法,链接如下:
其实也不是什么解决方法,只是把这个异常捕获了,不让他奔溃了,这个问题的终极解决方案还是得让google去修复。
1、创建一个类LinearLayoutManagerWrapper继承LinearLayoutManager,重写onLayoutChildren方法
- public class WrapContentLinearLayoutManager extends LinearLayoutManager {
- public WrapContentLinearLayoutManager(Context context) {
- super(context);
- }
- public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
- super(context, orientation, reverseLayout);
- }
- public WrapContentLinearLayoutManager(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) {
- e.printStackTrace();
- }
- }
- }
2、设置RecyclerView的布局管理为WrapContentLinearLayoutManager对象
- mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
我经过大量的测试,发现这个异常真的被catch了,没有再出现这种崩溃的问题了,大功告成,希望对大家有所帮助!!!
RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter的解决方案(转)的更多相关文章
- RecycleView错误: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder
1.错误 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positi ...
- 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时出现异常: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state:30
RecyclerView 存在的一个明显的 bug 一直没有修复: java.lang.IndexOutOfBoundsException: Inconsistency detected. Inval ...
- Recyclerview 出现 java.lang.IndexOutOfBoundsException: Inconsistency detected 异常
使用 RecyclerView 的时候报错 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view hold ...
- RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.
今天使用RecyclerView时,上下两个RecyclerView,在实现下拉刷新时,报错: java.lang.IndexOutOfBoundsException: Inconsistency d ...
- android view holder 优化
android 一般都用viewholder来优化contentView,采用sparseArray能够进一步优化 /** * 用法: ImageView bananaView = ViewHolde ...
- 编译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驱 ...
- 解析RecyclerView(2)——带顶部View和底部View的RecyclerView
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- 解决tensorflow 调用bug Running model failed:Invalid argument: NodeDef mentions attr 'dilations' not in Op<name=Conv2D; signature=input:T, filter:T ->
将tensorflow C++ 版本更新为何训练版本一致即可
随机推荐
- 六. 异常处理7.throw:异常的抛出
到目前为止,你只是获取了被Java运行时系统抛出的异常.然而,程序可以用throw语句抛出明确的异常.Throw语句的通常形式如下: throw ThrowableInstance;这里,Thr ...
- 合理配置SQL Server的最大内存
http://blog.itpub.net/26435490/viewspace-1481846/
- IdHTTPServer(indy10)开发REST中间件
IdHTTPServer(indy10)开发REST中间件 浏览器通过“get”方式查询数据URL样例:http://127.0.0.1:7777/query?sql=select * from t1 ...
- LCIS线段树(区间更新)
首先线段树每一个节点包含:[b,e],lmax,rmax,max;其中lmax表示从左端点开始连续的最长的增序列长度,rmax表示从e端点开始向左连续的最长下降序列长度,max表示当前区间的连续递增的 ...
- CURL简单使用
学习地址:https://yq.aliyun.com/articles/33262 curl的简单使用步骤 要使用cURL来发送url请求,具体步骤大体分为以下四步: 1.初始化2.设置请求选项3.执 ...
- 用ASP实现JS的decodeURIComponent()函数
<% response.write jsDecodeURIComponent( "%E6%B5%8B%E8%AF%95" ) %> <script languag ...
- C++之纯虚函数
1. 纯虚函数形式 class Parent { public: ; }; 代码中的func1就是纯虚函数,没有函数体,没有函数的具体实现,有virtual,在函数名后面带有“ = 0”形式: 2.对 ...
- 获取本机IP,用户代理
1.获取本机IP:http://httpbin.org/ip 2.获取用户代理 https://httpbin.org/user-agent https://httpbin.org/ httpbin( ...
- Android - 标准VideoView播放演示样例
标准VideoView播放演示样例 本文地址: http://blog.csdn.net/caroline_wendy 在Android SDK中的ApiDemos内, 提供标准播放视频的代码,使用V ...
- Oracle dos连接数据库基本操作
sqlplus / as sysdba;(sqlplus 用户名/密码@ip:端口:数据库实例 as sysdba;) ;(设置显示多少列,pagesize:;每页多少记录) select * fro ...