问题:E/RecyclerView:未连接适配器;跳过布局
我需要一个适配器来显示在回收器视图中。但它显示此错误 E/RecyclerView:未连接适配器;跳过布局。请帮助我,我是开发新手。我正在使用 kotlin 和fragment
`class UsersDashboardFragment : Fragment() {
private val binding: FragmentUsersDashboardBinding? = null
//local variable adapter
private var adapter: RecyclerView.Adapter<*>? = null
//class variable
private val featuredAdapter:FeaturedAdapter? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
activity?.window?.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
val binding: FragmentUsersDashboardBinding =
DataBindingUtil.inflate(
inflater, R.layout.fragment_users_dashboard, container, false
)
featuredRecycler()
return binding.root
}
private fun featuredRecycler() {
binding?.featuredRecycler?.setHasFixedSize(true)
binding?.featuredRecycler?.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
//sut up images in recycler view
val featuredLocations : ArrayList<FeaturedHelperClass> = ArrayList()
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd sfds dsfsfsdf "))
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd sfds dsfsfsdf "))
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd sfds dsfsfsdf "))
//call adapter
adapter = FeaturedAdapter(featuredLocations)
binding?.featuredRecycler?.adapter = featuredAdapter
}
}`
使用 RecyclerView 的布局文件
`<androidx.recyclerview.widget.RecyclerView
android:id="@+id/featured_recycler"
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_toRightOf="@id/featured_backgtound"
android:background="@color/govno" />`
适配器类
`public class FeaturedAdapter extends RecyclerView.Adapter<FeaturedAdapter.FeaturedViewHolder> {
ArrayList<FeaturedHelperClass> featuredLocations;
public FeaturedAdapter(ArrayList<FeaturedHelperClass> featuredLocations) {
this.featuredLocations = featuredLocations;
}
public static class FeaturedViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView title,desc;
public FeaturedViewHolder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.featured_image);
title = itemView.findViewById(R.id.featured_title);
desc = itemView.findViewById(R.id.featured_desc);
}
}
@NonNull
@Override
public FeaturedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feautured_card_design,parent,false);
FeaturedViewHolder featuredViewHolder = new FeaturedViewHolder(view);
return featuredViewHolder;
}
@Override
public void onBindViewHolder(@NonNull FeaturedViewHolder holder, int position) {
FeaturedHelperClass featuredHelperClass = featuredLocations.get(position);
holder.image.setImageResource(featuredHelperClass.getImage());
holder.title.setText(featuredHelperClass.getTitle());
holder.desc.setText(featuredHelperClass.getDescription());
}
@Override
public int getItemCount() {
return featuredLocations.size();
}
}`
我认为问题出在这段代码的片段中,但我不知道如何更改它
`
//sut up images in recycler view
val featuredLocations : ArrayList<FeaturedHelperClass> = ArrayList()
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd
sfds dsfsfsdf "))
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd
sfds dsfsfsdf "))
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd sfds dsfsfsdf "))
//call adapter
adapter = FeaturedAdapter(featuredLocations)
binding?.featuredRecycler?.adapter = featuredAdapter
}`
I need an Adapter to be displayed in the Recycler View. but it shows this error E/RecyclerView: No adapter attached; skipping layout. please help me i am new to development. i'm using kotlin and fragment
`class UsersDashboardFragment : Fragment() {
private val binding: FragmentUsersDashboardBinding? = null
//local variable adapter
private var adapter: RecyclerView.Adapter<*>? = null
//class variable
private val featuredAdapter:FeaturedAdapter? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
activity?.window?.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
val binding: FragmentUsersDashboardBinding =
DataBindingUtil.inflate(
inflater, R.layout.fragment_users_dashboard, container, false
)
featuredRecycler()
return binding.root
}
private fun featuredRecycler() {
binding?.featuredRecycler?.setHasFixedSize(true)
binding?.featuredRecycler?.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
//sut up images in recycler view
val featuredLocations : ArrayList<FeaturedHelperClass> = ArrayList()
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd sfds dsfsfsdf "))
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd sfds dsfsfsdf "))
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd sfds dsfsfsdf "))
//call adapter
adapter = FeaturedAdapter(featuredLocations)
binding?.featuredRecycler?.adapter = featuredAdapter
}
}`
layout file with RecyclerView
`<androidx.recyclerview.widget.RecyclerView
android:id="@+id/featured_recycler"
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_toRightOf="@id/featured_backgtound"
android:background="@color/govno" />`
Adapter class
`public class FeaturedAdapter extends RecyclerView.Adapter<FeaturedAdapter.FeaturedViewHolder> {
ArrayList<FeaturedHelperClass> featuredLocations;
public FeaturedAdapter(ArrayList<FeaturedHelperClass> featuredLocations) {
this.featuredLocations = featuredLocations;
}
public static class FeaturedViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView title,desc;
public FeaturedViewHolder(@NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.featured_image);
title = itemView.findViewById(R.id.featured_title);
desc = itemView.findViewById(R.id.featured_desc);
}
}
@NonNull
@Override
public FeaturedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feautured_card_design,parent,false);
FeaturedViewHolder featuredViewHolder = new FeaturedViewHolder(view);
return featuredViewHolder;
}
@Override
public void onBindViewHolder(@NonNull FeaturedViewHolder holder, int position) {
FeaturedHelperClass featuredHelperClass = featuredLocations.get(position);
holder.image.setImageResource(featuredHelperClass.getImage());
holder.title.setText(featuredHelperClass.getTitle());
holder.desc.setText(featuredHelperClass.getDescription());
}
@Override
public int getItemCount() {
return featuredLocations.size();
}
}`
I think the problem is in the fragment with this code, but I can't figure out how to change it
`
//sut up images in recycler view
val featuredLocations : ArrayList<FeaturedHelperClass> = ArrayList()
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd
sfds dsfsfsdf "))
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd
sfds dsfsfsdf "))
featuredLocations.add(FeaturedHelperClass(R.drawable.img_1,"Mcdonald's","sdfsdfsdfsdfs sdfd sfds dsfsfsdf "))
//call adapter
adapter = FeaturedAdapter(featuredLocations)
binding?.featuredRecycler?.adapter = featuredAdapter
}`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
featuredRecycler
函数中不断引用binding?
,但您从未为此属性分配任何内容,因此类似这样的操作:不会执行任何操作,因为
binding
代码> 为空。此行:
应该更改为,
以便您将绑定分配给您的属性,而不是分配给具有相同名称的某些局部变量。
You keep referencing
binding?
in yourfeaturedRecycler
function, but you never assigned anything to this property, so something like this:doesn't do anything because
binding
is null.This line:
should be changed to
so you are assigning the binding to your property, and not to some local variable with the same name.