Snippet with RecyclerView: java.lang.IllegalArgumentException: scraps or attached views cannot be recycled. isScrap: false isAttached: true

I keep getting java.lang.IllegalArgumentException: snippets or attached views cannot be recycled. isScrap: false isAttached: true when using Fragment with RecyclerView. I have only 1 activity that switches between multiple fragments. In the onCreate action, I set the default fragment, which, as it turned out, has a RecyclerView, implemented exactly the same as in the documentation. When the activity starts, I get java.lang.IllegalArgumentException: snippets or attached views cannot be processed. isScrap: false isAttached: true .

The problem is that at the beginning I load an empty container with fragments and then iterate over the fragment using RecyclerView, it works fine. Also, I do not use android: animateLayoutChanges or notifyDataSetChanged () , as indicated here . Currently I am setting up a RecyclerView in the fragment's onResume () method. I tried switching it to other life cycle methods, but not luck.

Any help is appreciated.

thanks

I added only the relevant code snippets. I believe this is due to some kind of life cycle, given the fact that it works if I do not set the fragment in onCreate () Activity. It worked when I had a ListView instead of a RecyclerView. I did not post the code for RecyclerView, because it is the same as in the documentation.

OnCreate action

public void onCreate(Bundle savedInstanceState) { Log.d(TAG,"### onCreate ###"); super.onCreate(savedInstanceState); setContentView(R.layout.efficientix_activity_layout); if(savedInstanceState != null){ checkedSideMenuItemLabel = savedInstanceState.getInt("checkedSideMenuItemLabel"); } //Init components initActionBar(checkedSideMenuItemLabel,savedInstanceState); initSideMenuArrayAdapter(); initSideMenu(checkedSideMenuItemLabel,savedInstanceState); initActionBarDrawerToggle(); fragmentManager = this.getSupportFragmentManager(); if(savedInstanceState == null){ //Set default fragment upon activity creation. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); DashboardFragment df = new DashboardFragment(); fragmentTransaction.replace(R.id.fragment_container,df,DashboardFragment.class.toString()); fragmentTransaction.commit(); } } 

Fragment onResume ()

 public void onResume() { super.onResume(); if (recylerViewLayoutManager == null) { recylerViewLayoutManager = new LinearLayoutManager(this.getActivity()); } recylerView.setLayoutManager(remindersLayoutManager); initRecylerViewAdapter(); recylerView.setAdapter(recylerViewAdapter); } 

Fragment Layout

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/myRecyclerView" style="@style/Main_Content_List_View"/> </LinearLayout> 

Action layout

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:efficientix="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The actionbar toolbar --> <include layout="@layout/actionbar_toolbar"/> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu_layout" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/fragment_container" android:orientation="vertical"> </LinearLayout> <include layout="@layout/side_menu_layout"/> </android.support.v4.widget.DrawerLayout> </LinearLayout> 

RecyclerView Element Layout

 <?xml version="1.0" encoding="utf-8"?> <android.widget.TableLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/Main_Content_Table_Layout_List_Item"> <TableRow style="@style/Main_Content_Table_Row_List_Item"> <TextView android:id="@+id/description" style="@style/Main_Content_TextView" /> <ImageView android:id="@+id/category" style="@style/Main_Content_ImageView"/> </TableRow> <TableRow style="@style/Main_Content_Table_Row_List_Item"> <TextView android:id="@+id/alert_date" style="@style/Main_Content_TextView" /> <TextView android:id="@+id/type" style="@style/Main_Content_TextView" /> </TableRow> </android.widget.TableLayout> 
+4
source share
2 answers

Ok, so I found that the problem was. In my ViewHolder, I had, in addition to the Views layout, an ORMlite object (maybe it was any object that was not part of the layout). The problem was that the ViewHolder equals () and hashcode () methods were based on an entity that was null. The reason this was null was because with RecyclerView you do not have access to the data position in onCreateViewHolder (), but only in onBindViewHolder (). The adapter data is a list of ORMlite objects in my case, and I want to link the object inside the holder. (Still need to find a way to do this ...)

In this case, I was expecting a NullPointerException. I managed to get a NullPointerException by calling the owner.setIsRecyclable (true) file.

Hope this helps others in need.

thanks

+2
source

I saw how this happened to me when I used a custom object in the ViewHolder for the Recycler View adapter.

To fix the problem, I cleared a custom object, which in my case was a timer in the onViewRecycled (ViewHolder holder) for the adapter, as shown below:

 public void onViewRecycled(ViewHolder holder) { if(holder instanceof EntityViewHolder) { if(((EntityViewHolder)holder).timer != null) { ((EntityViewHolder) holder).timer.cancel(); } } super.onViewRecycled(holder); } 

This fixed the bug.

0
source

Source: https://habr.com/ru/post/1258835/


All Articles