Inconsistency detected Invalid view holder adapter position"

RecyclerView在添加数据的时候发生了异常.

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{f82aa71 position=11 id=-1, oldPos=6, pLpos:6 scrap [attachedScrap] tmpDetached not recyclable(1) no parent}
     at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5297)
     at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5479)
     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
     at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
     at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
     at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
     at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3534)
     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3310)
  ...
复制代码

RecyclerView#validateViewHolderForOffsetPosition(ViewHolder holder)

if (holder.mPosition < 0 || holder.mPosition >= mAdapter.getItemCount()) {
                throw new IndexOutOfBoundsException("Inconsistency detected. Invalid view holder "
                        + "adapter position" + holder);
            }
复制代码

holder.mPosition >= mAdapter.getItemCount()为真,抛出了异常 发现mAdapter.getItemCount() 的值是正常的,但是holder.mPosition值有问题,在某种情况下和getItemCount的值一样大,此时条件为真。

接下来看了下RecyclerView中有5个方法改变了mPosition的值,分别是

  • Adapter#bindViewHolder()
  • ViewHolder#resetInternal()
  • ViewHolder#offsetPosition()
  • ViewHolder#flagRemovedAndOffsetPosition()
  • Recycler#tryGetViewHolderForPositionByDeadline()

分别打了断点,发现会引起崩溃的代码,会改变mPosition的有其中三个地方resetInternal()offsetPosition()bindViewHolder()resetInternal() 首先排除 ,因为mPosition = NO_POSITION;

然后仔细查看offsetPosition()bindViewholder()中的值发现,offsetPosition()的值mPosition += offset;会超出mPosition的值的范围。 所以现在就要去找这个方法被调用的时机和offset这个值的含义

  1. 查看offsetPosition()的调用,发现调用分别来自RecyclerView与RecyclerView.Recycler两个的对应的insert,moveremove方法,以及ViewHolderflagRemovedAndOffsetPosition(int, int, boolean)的方法 主要看就下面这个
void offsetPositionRecordsForInsert(int positionStart, int itemCount) {
  //...
   //这里将itemCount 传递给了offsetPosition
               holder.offsetPosition(itemCount, false);
 //...
       mRecycler.offsetPositionRecordsForInsert(positionStart, itemCount);
       requestLayout();
   }
复制代码

看起来在某个刷新数据的地方理解错了itemCount,找到我自己Adapter代码里面的 notifyItemRangeInserted(positionStart,itemCount);暂时修改为notifyDataSetChanged()不会报错了

转载于:https://juejin.im/post/5a31ee3a6fb9a045132ab606

发布了176 篇原创文章 · 获赞 33 · 访问量 20万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

帮忙看看这个android错误 !

08-03

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.lenovo.push, PID: 17578 java.lang.NullPointerException: Attempt to write to field 'int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null object reference at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6368) at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5555) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224) at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:556) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595) at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844) at android.view.View.layout(View.java:18799) at android.view.ViewGroup.layout(ViewGroup.java:5952) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:18799) at android.view.ViewGroup.layout(ViewGroup.java:5952) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:18799) at android.view.ViewGroup.layout(ViewGroup.java:5952) at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131) at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1391) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:870) at android.view.View.layout(View.java:18799) at android.view.ViewGroup.layout(ViewGroup.java:5952) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:18799) at android.view.ViewGroup.layout(ViewGroup.java:5952) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) at android.widget.LinearLayout.onLayout(LinearLayout.java:1494) at android.view.View.layout(View.java:18799) at android.view.ViewGroup.layout(ViewGroup.java:5952) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:18799) at android.view.ViewGroup.layout(ViewGroup.java:5952) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) at android.widget.LinearLayout.onLayout(LinearLayout.java:1494) at android.view.View.layout(View.java:18799) at android.view.ViewGroup.layout(ViewGroup.java:5952) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:822) at android.view.View.layout(View.java:18799) at android.view.ViewGroup.layout(ViewGroup.java:5952) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2627) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2343) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1502) E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7044) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927) at android.view.Choreographer.doCallbacks(Choreographer.java:702) at android.view.Choreographer.doFrame(Choreographer.java:638) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) Application terminated. 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览