本文主要是介绍Android报错:java.lang.IllegalStateException: ViewHolder views must not be attached when created.,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这个错误很奇怪,先记录下来,
错误信息如下,
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.spacesoftwares.myapplication3, PID: 11575
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)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)
解决方案在stackoverflow上面【1】,毕竟我的源码和人家源码不一样,我贴出来,全文如下,
package com.spacesoftwares.myapplication3.MyRecyclerView;import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;import com.spacesoftwares.myapplication3.R;import org.w3c.dom.Text;import java.util.List;public class LinearAdaptor extends RecyclerView.Adapter<LinearAdaptor.LinearViewHolder> {private Context mContext;public LinearAdaptor(Context context){this.mContext = context;}@NonNull@Overridepublic LinearAdaptor.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {//return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item, parent));View view = LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item, parent, false);return new LinearViewHolder(view);}@Overridepublic void onBindViewHolder(@NonNull LinearAdaptor.LinearViewHolder holder, int position) {holder.textView.setText("LinearViewHolder");}@Overridepublic int getItemCount() {return 20;}class LinearViewHolder extends RecyclerView.ViewHolder{private TextView textView;public LinearViewHolder(View itemView){super(itemView);textView = itemView.findViewById(R.id.li_title);}}
}
报错的部分在这里,
//return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item, parent));
我改写成下面的形式,就没有报错了,主要是inflate后面加了一个false,如下
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item, parent, false);
return new LinearViewHolder(view);
【1】https://stackoverflow.com/questions/50655791/a-difficulty-with-recyclerview-adapter-after-update-to-api-27
这篇关于Android报错:java.lang.IllegalStateException: ViewHolder views must not be attached when created.的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!