RecyclerView java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position when resume from added fragment

百般思念 提交于 2019-12-08 15:44:42

问题


Can you guys help me on this

here is my code to add 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();
                                    }

                                }

                            })
                    );

But when i press back from the added fragment i got this error and my app crash.

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)

why is this happening? please help. im using two fragment. the product list fragent and product details fragment


回答1:


you're calling mItems.clear(); without notifying the RecyclerView's adapter that something in the data had changed. after each change to the data used by the adapter you need to call adapter.notifyDataSetChanged();

See this.




回答2:


Try this

i was facing the same issue ,when you are clearing your list you have to notify the adapter because everytime you are changing the list and the adapter is containing old list so you have to notify the adapter after clearing the list before adding new items in adapter

  1. make instance of data members in starting oof your class

    ArrayList<HashMap<String,String>> yourarraylist =new ArrayList<>();
    Youradaptername instance;
    
  2. after clearing your list notify the adapter

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



回答3:


The issue may be related to the adapter. If you are using setHasStableIds(true); in the adapter constructor, you need to make sure that you are using stable ids.

Make sure that you override getItemId correctly in the adapter. It should be:

@Override
public long getItemId(int position) {
   return yourList == null ? 0 : yourList.get(position).getId();
}

And not:

@Override
public long getItemId(int position) {
     return position;
}


来源:https://stackoverflow.com/questions/36258889/recyclerview-java-lang-indexoutofboundsexception-inconsistency-detected-invali

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!