RecyclerView java.lang.IndexOutOfBoundsException:检测到不一致。 从添加的片段恢复时,无效的项目位置

你们能帮我吗?

这里是我的代码添加片段

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(); } } }) ); 

但是当我从添加的片段回来我得到这个错误,我的应用程序崩溃。

 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) 

为什么会这样呢? 请帮忙。 我使用两个片段。 产品清单和产品细节片段

Solutions Collecting From Web of "RecyclerView java.lang.IndexOutOfBoundsException:检测到不一致。 从添加的片段恢复时,无效的项目位置"

你正在调用mItems.clear(); 而不通知RecyclerView的适配器数据中的某些内容已经改变。 在每次更改适配器使用的数据之后,您需要调用adapter.notifyDataSetChanged();

看到这个

尝试这个

我面临着同样的问题,当你清除你的列表时,你必须通知适配器,因为每次你改变列表,并且适配器包含旧列表,所以你必须在清除列表之后通知适配器,然后在适配器中添加新的项目

  1. 在你的课堂上开始创build数据成员的实例

     ArrayList<HashMap<String,String>> yourarraylist =new ArrayList<>(); Youradaptername instance; 
  2. 清除清单后通知适配器

      yourarraylist .clear(); instance.notifyDataSetChanged();