ViewHolder views must not be attached when created. Ensure that you are not passing ‘true‘ to the at

2021-02-08 15:41:57.540 15876-15876/cn.xiayiye5.xiayiye5library E/AndroidRuntime: FATAL EXCEPTION: main
    Process: cn.xiayiye5.xiayiye5library, PID: 15876
    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 androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7080)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:675)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
        at android.view.View.measure(View.java:25466)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
        at android.view.View.measure(View.java:25466)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:490)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)

 

ViewHolder views must not be attached when created. Ensure that you are not passing ‘true‘ to the at

 

上面是我的报错日志,我这边遇到这个问题原因也非常简单,意思是说我加载的这个布局的外层没有父布局,通过我检查代码发现我的写法有错误:

 @NonNull
    @Override
    public XiaYiYeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        xiaYiYeHolder = new XiaYiYeHolder(view);
        return xiaYiYeHolder;
    }


    protected XiaYiYeAdapter(Context context,@LayoutRes int layoutId, List<E> data) {
       view= View.inflate(context,layoutId,null);
        this.layoutId = layoutId;
        this.data = data;
    }

ViewHolder views must not be attached when created. Ensure that you are not passing ‘true‘ to the at

看下更改后的代码:

  @NonNull
    @Override
    public XiaYiYeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
        xiaYiYeHolder = new XiaYiYeHolder(view);
        return xiaYiYeHolder;
    }


    protected XiaYiYeAdapter(@LayoutRes int layoutId, List<E> data) {
        this.layoutId = layoutId;
        this.data = data;
    }

 

ViewHolder views must not be attached when created. Ensure that you are not passing ‘true‘ to the at

好了完工项目正常跑起来了!!

上一篇:Python-Modules-Peewee


下一篇:闲聊Kubernetes Pod垂直自动伸缩(VPA)