IndexOutOfBoundException with RecyclerView

谁都会走 提交于 2020-12-06 23:56:32

问题


I am experiencing a problem with my application. I am getting a java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder and not only on Samsung device but also on LG because I have read somewhere that this exception only happens on Samsung device.

My solution was to make my own LinerLayoutManager and to catch the exception in onLayoutChildren() method. After implementing this solution the exception doesn't occur very often like before but still sometimes ocurrs when I close the app and start it again immediately after closing.

Can someone help me with this problem?

Here is my LinearLayoutManager:

 public class WrapContentLinearLayoutManager extends LinearLayoutManager {


  public WrapContentLinearLayoutManager(Context context) {
    super(context);
 }

  public WrapContentLinearLayoutManager(Context context, AttributeSet attrs,    int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
}

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    try {
        super.onLayoutChildren(recycler, state);
    } catch (IndexOutOfBoundsException e) {
        Timber.e("catching recycler view IndexOutOfBoundsException = " + e);
     }
 }

 @Override
 public boolean supportsPredictiveItemAnimations() {
     return false;
 }
}

and in the MainFragment:

    LinearLayoutManager linearLayoutManager = new WrapContentLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(linearLayoutManager);

Stacktrace:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{27dd5184 position=1 id=-1, oldPos=-1, pLpos:-1 no parent}
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5041)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5172)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5153)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
 at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1225)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1045)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4579)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

来源:https://stackoverflow.com/questions/41300626/indexoutofboundexception-with-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!