Android之RecyclerView的原生Bug-Inconsistency detected. Invalid view holder adapter positionViewHolder{a1bbfa3 position=2 id=-1, oldPos=-1, pLpos:-1 no parent},recyclerviewholder

來源:互聯網
上載者:User

Android之RecyclerView的原生Bug-Inconsistency detected. Invalid view holder adapter positionViewHolder{a1bbfa3 position=2 id=-1, oldPos=-1, pLpos:-1 no parent},recyclerviewholder

今天在運行自己編寫的App時,突然發現App在運行時閃退,然後就查看了Android Studio的Log,發現了這個錯誤,上網查了一下,才知道是RecyclerView的原生Bug,在資料更新時會出現這個Bug,錯誤顯示為

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)  

可以看到錯誤並沒有指到我自己所寫的代碼,所以斷定是原生的Bug。

錯誤解決的方式是自訂一個LayoutManager,重寫onLayoutChildren方法,在遇到這個異常時,直接拋出異常。

代碼為

package com.zhangmiao.developmenthelperdemo;import android.content.Context;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.util.AttributeSet;/** * Created by zhangmiao on 2016/12/7. */public class MyContentLinearLayoutManager extends LinearLayoutManager {    public MyContentLinearLayoutManager(Context context){        super(context);    }    public MyContentLinearLayoutManager(Context context,int orientation,boolean reverseLayout){        super(context,orientation,reverseLayout);    }    public MyContentLinearLayoutManager(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的setLayoutManager的參數修改為WrapContentLinearLayoutManager。

代碼如下:

 recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mView.getContext()));

 

參考文章:http://blog.csdn.net/lovexieyuan520/article/details/50537846

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.