Android - java.lang.IndexOutOfBoundsException:无效的项目位置 0(0)。项目数:0

Posted

技术标签:

【中文标题】Android - java.lang.IndexOutOfBoundsException:无效的项目位置 0(0)。项目数:0【英文标题】:Android - java.lang.IndexOutOfBoundsException: Invalid item position 0(0). Item count:0 【发布时间】:2016-08-24 08:18:42 【问题描述】:

我的日志中出现了这个异常 -

 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)

我遇到了这个错误 -

View view = recycler.getViewForPosition(position);

这里,在自定义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);

        
    

以前,它运行良好。 我最近将 targetSdkVersion 更改为 23(Marshmallow),以及支持 lib 的版本。可能是出于同样的原因。

请指导我,如何解决这个问题。

【问题讨论】:

你在某处使用过notifyDataSetChanged 吗? 不,我没有在任何地方使用 notifyDataSetChanged。但我在 RecyclerView 上调用 viewTreeObserver, 改用findViewByPosition会不会受影响? 不,我不认为。作为我到达那里的位置,在方法中 - measureScrapChild 是 0 我想,我必须尽量避免得到 0 【参考方案1】:

我遇到了同样的问题,我找到了更好的方法来解决它。

在自定义 LayoutManager 的构造函数中调用 setAutoMeasureEnabled(false);

API 级别 27 之后 setAutoMeasureEnabled() 已弃用。 因此,只需在您的自定义 LinearLayoutManager 中覆盖此方法

@Override
public boolean isAutoMeasureEnabled() 
    return false;

【讨论】:

在将 RecyclerView 从 v7:21.0.0 更新到 v7:27.0.2 后,我遇到了这个问题。通过使用建议的解决方案,它终于奏效了。他们必须从 v7:22 更改了 RecyclerView 的默认行为。谢谢。 自 v7:27.1.0 起,此方法已被弃用。你应该在你的实现中覆盖isAutoMeasureEnabled()。 developer.android.com/reference/android/support/v7/widget/…【参考方案2】:

终于解决了:)

更换

编译'com.android.support:recyclerview-v7:23.2.1'

编译'com.android.support:recyclerview-v7:22.+'

工作。

不确定,这是否是完美的解决方案,但它对我有用。

【讨论】:

不行,需要用构建工具编译 23.

以上是关于Android - java.lang.IndexOutOfBoundsException:无效的项目位置 0(0)。项目数:0的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )

android 21 是啥版本

Android逆向-Android基础逆向(2-2)

【Android笔记】android Toast

图解Android - Android核心机制

Android游戏开发大全的目录