问题:E/RecyclerView:未连接适配器;跳过布局

发布于 2025-01-18 21:11:54 字数 4309 浏览 0 评论 0原文

我需要一个适配器来显示在回收器视图中。但它显示此错误 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2025-01-25 21:11:55

您在 featuredRecycler 函数中不断引用 binding?,但您从未为此属性分配任何内容,因此类似这样的操作:

binding?.featuredRecycler?.adapter = featuredAdapter

不会执行任何操作,因为 binding代码> 为空。

此行:

val binding: FragmentUsersDashboardBinding =

应该更改为,

binding: FragmentUsersDashboardBinding =

以便您将绑定分配给您的属性,而不是分配给具有相同名称的某些局部变量。

You keep referencing binding? in your featuredRecycler function, but you never assigned anything to this property, so something like this:

binding?.featuredRecycler?.adapter = featuredAdapter

doesn't do anything because binding is null.

This line:

val binding: FragmentUsersDashboardBinding =

should be changed to

binding: FragmentUsersDashboardBinding =

so you are assigning the binding to your property, and not to some local variable with the same name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文