java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 的解决方案

最近发现应用在线上报了一个bug,乍一看,定位不到我们的代码问题,但是就是crash了,自己也是一脸懵逼,后来经过网上资料查找,这个问题不是我们的代码问题,是Recyclerview内部的bug,你敢信!!!,Google的东西都有bug..............


话不多说,看下下面的报错:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 10(offset:0).state:11
	at com.ushareit.playit.qr.a(SourceFile:4503)
	at com.ushareit.playit.qr.c(SourceFile:4461)
	at com.ushareit.playit.pq.a(SourceFile:1962)
	at android.support.v7.widget.GridLayoutManager.a(SourceFile:438)
	at android.support.v7.widget.LinearLayoutManager.a(SourceFile:1334)
	at android.support.v7.widget.LinearLayoutManager.c(SourceFile:563)
	at android.support.v7.widget.GridLayoutManager.c(SourceFile:171)
	at android.support.v7.widget.RecyclerView.k(SourceFile:2801)
	at android.support.v7.widget.RecyclerView.onLayout(SourceFile:3145)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
	at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.support.v4.view.ViewPager.onLayout(SourceFile:1627)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.support.v4.widget.DrawerLayout.onLayout(SourceFile:1043)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
	at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
	at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
	at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
	at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888)
	at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742)
	at android.widget.LinearLayout.onLayout(LinearLayout.java:1651)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
	at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
	at android.view.View.layout(View.java:15125)
	at android.view.ViewGroup.layout(ViewGroup.java:4862)
	at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2323)
	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029)
	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
	at android.view.Choreographer.doCallbacks(Choreographer.java:591)
	at android.view.Choreographer.doFrame(Choreographer.java:560)
	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
	at android.os.Handler.handleCallback(Handler.java:808)
	at android.os.Handler.dispatchMessage(Handler.java:103)
	at android.os.Looper.loop(Looper.java:193)
	at android.app.ActivityThread.main(ActivityThread.java:5292)
	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:824)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
	at dalvik.system.NativeStart.main(Native Method)
出现这个问题的原因和重现的方法是:

使用RecyclerView加官方下拉刷新的时候,如果绑定的List对象在更新数据之前进行了clear,而这时用户紧接着迅速上滑 RV,就会造成崩溃,而且异常不会报到你的代码上,属于RV内部错误。初次猜测是,当你clear了list之后,这时迅速上滑,而新数据还没到来,导致Recyclerview要更新加载下面的Item时候,找不到数据源了,造成crash.

但明显,更新数据之前clear list是挺常见的做法,你不可能祈祷用户这时候乖乖不动等待新数据加载完,所以根本就是不合理的。

查找资料,有些说的解决方法是,就是在刷新,Recyclerview  clear的同时让Recyclerview不能滑动,这样的解决办法也是可以的,可以避免错误的发生,但是我总感觉这样的会影响用户的体验,该解决办法的代码如下:

mRecyclerView.setOnTouchListener(
        new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mIsRefreshing) {
                    return true;
                } else {
                    return false;
                }
            }
        }
);


还有一种解决办法就是,我们写一个WrapContentLinearLayoutManager去继承LinearLayoutManager,在出现问题的时候我们catch了,这样的处理方法我觉得还是可以的,具体实现如下:

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();  
        }  
    }  
}  


然后设置Recyclerview的Manager为我们自己的Manager:

mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));  


然后亲自测试,这个问题真的被catch了,程序没有再crash掉,两种方法都可以,看你到底想要用那个方法解决了,其实这两种方法都是网上大神的作品,我只不过是借用,然后测试解决问题罢了,特此写一篇帖子来记录自己的一些问题和总结~





你可能感兴趣的:(个人总结)