ViewHolder views must not be attached when created

22,457

Solution 1

Actually, your ViewHolder expects a View inflated from R.layout.text_row_item rather than a descendant of the latter. So, if you pass the inflated view the problem will be resolved.

So, you should correct your code to this:

public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.ViewHolder> {

    private String[] mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ViewHolder(View v) {
            super(v);
            mTextView = v.findViewById(r.id.display_name);
        }
    }

    public MyRvAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    @NonNull
    @Override
    public MyRvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.text_row_item, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull MyRvAdapter.ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset[position]);
    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

Solution 2

In my case, I fixed it changing this line

val view = parent.inflate(R.layout.your_item)

to

val view = parent.inflate(R.layout.your_item, false)

Solution 3

Wait till view is created before binding.

Remove TextView userNameInList= v.findViewById(R.id.display_name); from your MyRvAdapter.ViewHolder.

And bind in ViewHolder as mTextView = v.findViewById(r.id.display_name);.

Solution 4

I know its too late but i want to add my answer, As it can help anybody who is facing this issue not due to above reason. In My Case i was using "Data Binding Library" and my mistake was that my root element was not direct child of <layout>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <data>

            <variable
                name="viewmodel"
                type="com.myapp.data.ViewModel" />
        </data>
        <!-- UI layout's root element -->
        <ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <!-- By Mistake i was Making this element as Root -->
            </FrameLayout>
        </ConstraintLayout>

    </layout>
Share:
22,457

Related videos on Youtube

Nakash.i
Author by

Nakash.i

Updated on June 17, 2020

Comments

  • Nakash.i
    Nakash.i about 4 years

    I'm trying to create a simple RV that will show a TextView. This is my adapter:

    public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.ViewHolder> {
    
    private String[] mDataset;
    
    
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }
    
    public MyRvAdapter(String[] myDataset) {
        mDataset = myDataset;
    }
    
    @NonNull
    @Override
    public MyRvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.text_row_item, parent, false);
        TextView userNameInList= v.findViewById(R.id.display_name);
        ViewHolder vh = new ViewHolder(userNameInList);
        return vh;
        }
    
    @Override
    public void onBindViewHolder(@NonNull MyRvAdapter.ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset[position]);
    }
    
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
    

    }

    text_row_item is just a FrameLayout with a TextView inside it("display_name"). This is the eror:

    java.lang.IllegalStateException: ViewHolder views must not be attached when created. Ensure that you are not passing 'true' to the attachToRoot parameter of LayoutInflater.inflate(..., boolean attachToRoot)
        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6687)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
    

    Thank you very much!

  • Maseed
    Maseed almost 5 years
    Google Android Guide was passing TextView to it but that was the directly inflated TextView, I was also confused. Thanks! developer.android.com/guide/topics/ui/layout/recyclerview
  • Marcell
    Marcell almost 4 years
    He already had attachToRoot set to false, so this was a different problem.
  • Cabezas
    Cabezas almost 4 years
    I understand your point of you @ohgodnotanotherone , that his problem is different but error´s name is the same like my problem. So this is the reason I put: In my case, I fixed it changing this line.
  • Moustafa EL-Saghier
    Moustafa EL-Saghier over 2 years
    Do you mean i've to add the constraint into frame layout?
  • Intsab Haider
    Intsab Haider over 2 years
    Whatever is your root view will be direct child of <layout>
  • Moustafa EL-Saghier
    Moustafa EL-Saghier over 2 years
    I do that but getting the same exception asking me to make sure attach to parent not set to true even I put false, I'm trying to reuse the generated databindingview to avoid the reinflate process