Android RecyclerView:IndexOutOfBoundsException:检测到不一致

时间:2017-06-09 02:12:47

标签: android android-recyclerview indexoutofboundsexception

我的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)

1 个答案:

答案 0 :(得分:3)

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

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