ViewHolder views must not be attached when created

江枫思渺然 提交于 2021-02-04 14:28:29

问题


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!


回答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;
    }
}



回答2:


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);.




回答3:


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>



回答4:


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)



回答5:


try this

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

private Context mContext;
private String[] mDataset;

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView title;

    MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.display_name);
    }
}

public MyRvAdapter(Context mContext, String[] myDataset) {
    this.mContext = mContext;
    this.mDataset = myDataset;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_row_item, parent, false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    holder.title.setText(mDataset[position]);
}

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


来源:https://stackoverflow.com/questions/51099529/viewholder-views-must-not-be-attached-when-created

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