IndexOutOfBoundException with RecyclerView
Asked Answered
E

0

1

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)
Elsy answered 23/12, 2016 at 11:30 Comment(8)
Please share your codeBenzophenone
please post your code with error logAsgard
@ApurvaKolapkar I've edited the question with the code and error logElsy
Did your problem become solve? i also have same problemTune
Help please same issueEtching
yeah, question still good even in 2020 - trying to handle this kind of exception.Distant
Still good in 2021 too.Sharyl
is there any solution?Susian

© 2022 - 2024 — McMap. All rights reserved.