Android - java.lang.IndexOutOfBoundsException: Invalid item position 0(0). Item count:0
R

2

12

I am getting this exception in my log -

 java.lang.IndexOutOfBoundsException: Invalid item position 0(0). Item count:0
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4622)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
            at com.jivemap.app.CustomizedViews.MyLinearLayoutManager.measureScrapChild(MyLinearLayoutManager.java:66)
            at com.jivemap.app.CustomizedViews.MyLinearLayoutManager.onMeasure(MyLinearLayoutManager.java:30)
            at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2614)
            at android.view.View.measure(View.java:15318)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413)
            at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1061)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:599)
            at android.view.View.measure(View.java:15318)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:704)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:597)
            at android.view.View.measure(View.java:15318)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413)
            at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1061)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:599)
            at android.view.View.measure(View.java:15318)
            at android.widget.ListView.setupChild(ListView.java:1849)
            at android.widget.ListView.makeAndAddView(ListView.java:1764)
            at android.widget.ListView.fillSpecific(ListView.java:1320)
            at android.widget.ListView.layoutChildren(ListView.java:1635)
            at android.widget.AbsListView.onLayout(AbsListView.java:2141)
            at android.view.View.layout(View.java:13874)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:596)
            at android.view.View.layout(View.java:13874)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1695)
            at android.view.View.layout(View.java:13874)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1672)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1530)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
            at android.view.View.layout(View.java:13874)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:13874)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1672)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1530)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
            at android.view.View.layout(View.java:13874)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:13874)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1672)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1530)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
            at android.view.View.layout(View.java:13874)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:13874)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:13874)
            at android.view.ViewGroup.layout(ViewGroup.java:4364)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)

I am getting this error on the line -

View view = recycler.getViewForPosition(position);

Here, in this method of custom LinearLayoutManager -

  private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
                                   int heightSpec, int[] measuredDimension) {



        View view = recycler.getViewForPosition(position);
        if (view != null) {
            RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
            int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
                    getPaddingLeft() + getPaddingRight(), p.width);
            int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
                    getPaddingTop() + getPaddingBottom(), p.height);
            view.measure(childWidthSpec, childHeightSpec);
            measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
            measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
            recycler.recycleView(view);

        }
    }

Previously, it was working well. I recently changed the targetSdkVersion to 23 (Marshmallow), and as well as the versions of support lib. May be the issue is for the same reason.

Please guide me, how to fix this.

Radiochemistry answered 29/4, 2016 at 8:50 Comment(15)
did you somewhere use notifyDataSetChanged ?Erogenous
No, I am not using notifyDataSetChanged anywhere. but I am calling viewTreeObserver on RecyclerView,Radiochemistry
Will it be affected if you use findViewByPosition instead ?Erogenous
no, I don't think. As the position I am getting there, in the method - measureScrapChild is 0Radiochemistry
I guess, I must try to avoid getting 0Radiochemistry
use findViewByPosition() on recyclerView.getLinearlayoutManager(). It will give the view at a particular index.Whorled
I don't have RecyclerView object inside measureScrapChild(). What I have there is - object of RecyclerView.Recycler. Thats why, I am unable to get LayoutManager. How to use findViewByPosition then.Radiochemistry
You may know that getViewForPosition method should be used by RecyclerView.LayoutManager where findViewByPosition() method easily available.Erogenous
let me post that method too. Hope, that will make you easy to understand.Radiochemistry
added the method @ShreeKrishna. Please check, I don't have RecyclerView there.Radiochemistry
@DroidWormNarendra Where this method belongs ? Please reply fast And sorry to inform I am leaving now and in leave for 2 days..Erogenous
@DroidWormNarendra This is onMeasure, not measureScrapChild..Erogenous
It belongs to custom LinearLayoutManager class, I already toldRadiochemistry
@ShreeKrishna check my exception log, it will tell, first onMeasure will be called, which in turn calls measureScrapChildRadiochemistry
@NarendraJi Did you find the answer for this ? I have the same problem, pl let us know how to solve this.Wreckfish
S
18

I meet the same problem, and I find a better way to solve it.

call setAutoMeasureEnabled(false); in the constructor of your custom LayoutManager.

After API level 27 setAutoMeasureEnabled() is deprecated. So just override this method in your Custom LinearLayoutManager

@Override
public boolean isAutoMeasureEnabled() {
    return false;
}
Scotland answered 29/6, 2016 at 8:55 Comment(2)
After updating RecyclerView from v7:21.0.0 to v7:27.0.2 i faced this issue. By using the proposed solution it finally worked. They must have changed the default beahavior of the RecyclerView from v7:22. Thank you.Murmuration
This method has been deprecated since v7:27.1.0. You should override isAutoMeasureEnabled() in your implementation. developer.android.com/reference/android/support/v7/widget/…Thurmanthurmann
R
0

Finally resolved :)

Replacing

compile 'com.android.support:recyclerview-v7:23.2.1'

to

compile 'com.android.support:recyclerview-v7:22.+'

worked.

Not Sure, this is perfect solution or not, but it worked for me.

Radiochemistry answered 29/4, 2016 at 12:6 Comment(1)
It doesn't work you need to compile with build tools 23.Bitumen

© 2022 - 2024 — McMap. All rights reserved.