当前位置: 首页 > 知识库问答 >
问题:

检测到不一致。无效的项目位置 0(偏移量:-1)...RecyclerView 在删除第一个项目时崩溃

严恩
2023-03-14

我正在尝试删除我的< code > recycle view 中的第一个项目。使用livedata和diffutil监控对列表的更改。仅当删除< code > recycle view 中的第一个项目(第0个索引)时,才会发生崩溃。这似乎与其他相关问题相似。快速滚动时不会出现此问题,等等。Ot仅在移除第0个位置的项目时发生。项目本身将从数据库中删除。但是应用程序崩溃了。我尝试从< code > recycle view 类中追溯错误:< code >。tryGetViewHolderForPositionByDeadline(recycle view . Java:5923)

来自RecyclerView.java:

if (offsetPosition < 0 || offsetPosition >= mAdapter.getItemCount()) {
            throw new IndexOutOfBoundsException("Inconsistency detected. Invalid item "
                    + "position " + position + "(offset:" + offsetPosition + ")."
                    + "state:" + mState.getItemCount());
        }

偏移位置小于0 (-1)

完整错误堆栈:

03-23 08:55:38.739 25087-25087/com.application.essentials E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.application.essentials, PID: 25087
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 0(offset:-1).state:3 android.support.v7.widget.RecyclerView{41df14f0 VFED.V.. ......ID 0,0-480,606 #7f080084 app:id/recycler_view}, adapter:com.application.essentials.model.AlarmsAdapter@41dd7d80, layout:android.support.v7.widget.LinearLayoutManager@41e10b80, context:com.application.essentials.MainActivity@41d37840
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5923)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:622)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:15819)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1183)
    at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:870)
    at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:889)
    at android.view.View.layout(View.java:15819)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1775)
    at android.view.View.layout(View.java:15819)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
    at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
    at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
    at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
    at android.view.View.layout(View.java:15819)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:15819)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
    at android.view.View.layout(View.java:15819)
    at android.view.ViewGroup.layout(ViewGroup.java:4884)...

共有1个答案

井旺
2023-03-14

问题出在适配器上。我在适配器构造器中使用setHasStableIds(true);以避免“更改时项目闪烁”。我覆盖了2个方法:public long getItemId(int位置){...}和public int getItemViewType(int位置){...}

崩溃消失,第0项被删除,有一次我删除了那行:setHasStableIds(true);在构造函数中。但是这些东西在闪烁。问题是我在被覆盖的方法中返回了什么。下面的代码有一个问题:

    @Override
public long getItemId(int position) {
     return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}

我返回的位置不是唯一的。我将项目的id作为唯一id返回,问题消失了。

正确代码:

    @Override
public long getItemId(int position) {
   return mAlarmEntityList.get(position).getAlarmId();
}

@Override
public int getItemViewType(int position) {
    return position;
}

我花了几个小时来找出问题所在。这些答案帮助了我:在回收器视图中检测到不一致,如何在滚动时更改回收器视图的内容

如何防止 RecyclerView 项目在通知ItemChanged(pos)后闪烁?

希望这对某人有帮助!

 类似资料: