gpt4 book ai didi

Android - java.lang.IndexOutOfBoundsException : Invalid item position 0(0). 项目数:0

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:42 27 4
gpt4 key购买 nike

我在日志中收到此异常 -

 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),以及支持库的版本。可能是出于同样的原因。

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

最佳答案

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

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

在 API 级别 27 之后 setAutoMeasureEnabled() 被弃用。所以只需在您的 Custom LinearLayoutManager 中覆盖此方法

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

关于Android - java.lang.IndexOutOfBoundsException : Invalid item position 0(0). 项目数:0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36933787/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com