gpt4 book ai didi

android - RecyclerView java.lang.IndexOutOfBoundsException : Inconsistency detected. 从添加的 fragment 恢复时项目位置无效

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

你们能帮帮我吗

这是我添加 fragment 的代码

mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
if (!IsScrolling) {
Fragment fragment = new ProductInfoFragment();
Bundle bundle = new Bundle();
bundle.putString("prodID", mItems.get(position).getproductID());
bundle.putString("catName", catName);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.add(R.id.fragmentContainer, fragment, "ProuctInfoFragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
mItems.clear();
}

}

})
);

但是当我从添加的 fragment 中按回时出现此错误并且我的应用程序崩溃了。

FATAL EXCEPTION: main Process: com.tiseno.poplook, PID: 2617
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 5(offset:5).state:9
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4401)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:438)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1161)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1018)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:3807)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5514)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

为什么会这样?请帮忙。我正在使用两个 fragment 。产品列表 fragment 和产品详情 fragment

最佳答案

您在调用 mItems.clear(); 时没有通知 RecyclerView 的适配器数据中的某些内容已更改。每次更改适配器使用的数据后,您需要调用 adapter.notifyDataSetChanged();

参见 this .

关于android - RecyclerView java.lang.IndexOutOfBoundsException : Inconsistency detected. 从添加的 fragment 恢复时项目位置无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36258889/

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