gpt4 book ai didi

Android RecyclerView : IndexOutOfBoundsException: Inconsistency detected

转载 作者:行者123 更新时间:2023-11-29 16:57:34 27 4
gpt4 key购买 nike

我的 RecyclerView 有问题,我了解到 here 这个错误已经修复,我应该确保我在主线程上插入数据并调用适当的通知。我这样做了,但我仍然收到此异常。

onLoadFinisher 方法(我正在使用加载器加载更多数据)

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data)
{
switch (loader.getId())
{
case UP_LOADER_ID:
Log.d("Adapter", "Thread: "+Thread.currentThread().getName());
days.put(firstDate, data);
notifyItemRangeInserted(0, data.getCount()+1);
loadingUp = false;
break;

case DOWN_LOADER_ID:
days.put(lastDate, data);
notifyItemRangeInserted(getItemCount(), data.getCount()+1);
loadingDown = false;
break;

default:
throw new IllegalArgumentException("Unknown loader id: " + loader.getId());
}
}

日志

I/OpenGLRenderer: Initialized EGL, version 1.4
D/Adapter: Thread: main
D/Adapter: Thread: main
D/Adapter: Thread: main
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.antilopa, PID: 6406
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{fa4fb40 position=42 id=-1497045600000, oldPos=21, pLpos:21 scrap [attachedScrap] tmpDetached not recyclable(1) no parent}
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5220)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5402)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3457)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3252)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767)
at android.view.View.layout(View.java:16722)
at android.view.ViewGroup.layout(ViewGroup.java:5438)

最佳答案

您可以将getItemCount()替换为getItemCount()-1,因为getItemCount()返回的是计数,但索引值为getItemCount()-1。

 case DOWN_LOADER_ID:
days.put(lastDate, data);
notifyItemRangeInserted(data.getCount()-1, data.getCount()+1);
loadingDown = false;
break;

关于Android RecyclerView : IndexOutOfBoundsException: Inconsistency detected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44448235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com