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,
错误解决的方式是自定义一个LayoutManager,重写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 (Exception e) {
e.printStackTrace();
}
}
}
然后将RecyclerView的setLayoutManager的参数修改为WrapContentLinearLayoutManager。
代码如下:
recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mView.getContext()));
还有一种异常:
java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder{ec75dcb position=18 id=-1, oldPos=-1, pLpos:-1 not recyclable(1)} android.support.v7.widget.RecyclerView{b3515af VFED..... ......ID 0,0-1080,1230 #7f0900ec app:id/recycleView}, adapter:com.xgh.app.demo2.CouponListAdapterdemo@68fbdbc, layout:com.xgh.app.demo2.WrapContentLinearLayoutManager@26b4145, context:com.xgh.app.demo2.DemoActivity@6973c1d
at android.support.v7.widget.RecyclerView4.processDisappeared(RecyclerView.java:531)
at android.support.v7.widget.ViewInfoStore.process(ViewInfoStore.java:242)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3789)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3447)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:611)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1762)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1606)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1515)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1762)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1606)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1515)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:774)
at android.view.View.layout(View.java:17702)
at android.view.ViewGroup.layout(ViewGroup.java:5631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2519)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2234)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1372)
at android.view.ViewRootImplCallbackRecord.run(Choreographer.java:926)
at android.view.Choreographer.doCallbacks(Choreographer.java:735)
at android.view.Choreographer.doFrame(Choreographer.java:667)
at android.view.ChoreographerMethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
参考了各种资料,list的数据,要和adpater数据同步,例如,list.clear(),执行后,要立即调用notifyDataSetChanged(),而不是等到网络请求完后,在调用notifyDataSetChanged()。意思是每当list集合改变时,就立马调用通知。
测试后不再报异常。希望对大家有帮助。