E/RecyclerView:没有附加适配器;跳过布局。尽管初始化适配器

Posted

技术标签:

【中文标题】E/RecyclerView:没有附加适配器;跳过布局。尽管初始化适配器【英文标题】:E/RecyclerView: No adapter attached; skipping layout. Despite initializing adapter 【发布时间】:2020-04-12 15:17:49 【问题描述】:

在this guide 之后,我尝试在片段中实现RecycleView。我得到了错误 E/RecyclerView: No adapter attached; skipping layout 回收视图不显示任何内容 我试过了recyclerview No adapter attached; skipping layoutRecyclerView in Fragment : No adapter attached skipping layout 还有更多。

我的代码:

package com.example.celebrity_proper;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;


/**
 * A simple @link Fragment subclass.
 */
public class MainFragment extends Fragment 

    private RecyclerView choiceRecycleView2 = null;
    private ChoiceAdapter adapter = null;
    private ArrayList<Choice> choiceArrayList;

    public MainFragment() 
        // Required empty public constructor
    

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        Log.d("Go here", "41");
        View view =  inflater.inflate(R.layout.fragment_main, container, false);

        choiceRecycleView2 = view.findViewById(R.id.choiceRecycleView);
        choiceRecycleView2.setLayoutManager(new LinearLayoutManager(getActivity()));
        choiceArrayList = new ArrayList<>();
        adapter = new ChoiceAdapter(getActivity(), choiceArrayList);
        choiceRecycleView2.setAdapter(adapter);
        createListData();

        return view;
    


    public void createListData() 
        Choice choice = new Choice("Adolf Hitler");
        choiceArrayList.add(choice);
        choice = new Choice("Kim Jong Un");
        Log.d("Item Count", String.valueOf(adapter.getItemCount()));
        choiceArrayList.add(choice);
        adapter.notifyDataSetChanged();
    



class Choice 
    private String name;

    public Choice(String name) 
        this.name = name;
    

    public String getName() 
        return this.name;
    


class ChoiceAdapter extends RecyclerView.Adapter<ChoiceViewHolder> 

    private Context context;

    private ArrayList<Choice> choices;
    public ChoiceAdapter(Context context, ArrayList<Choice> choices) 
        this.context = context;
        this.choices = choices;
    

    @NonNull
    @Override
    public ChoiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(context).inflate(R.layout.choice_row, parent, false);
        return new ChoiceViewHolder(view);
    

    @Override
    public void onBindViewHolder(@NonNull ChoiceViewHolder holder, int position) 
        Choice choice = choices.get(position);
        holder.setDetails(choice);
    

    @Override
    public int getItemCount() 
        return choices.size();
    


class ChoiceViewHolder extends RecyclerView.ViewHolder 
    private TextView choice_row_text;

    public ChoiceViewHolder(View view) 
        super(view);

        choice_row_text = view.findViewById(R.id.choice_row_text);
    

    public void setDetails(Choice choice) 
        choice_row_text.setText(choice.getName());
    

【问题讨论】:

【参考方案1】:

您正在向适配器构造函数发送空列表。正道


        choiceRecycleView2 = view.findViewById(R.id.choiceRecycleView);
        choiceRecycleView2.setLayoutManager(new LinearLayoutManager(getActivity()));
        choiceArrayList = new ArrayList<>();

        createListData();

        adapter = new ChoiceAdapter(getActivity(), choiceArrayList);
        choiceRecycleView2.setAdapter(adapter);
        adapter.notifyDataSetChanged();


    public void createListData() 
        Choice choice = new Choice("Adolf Hitler");
        choiceArrayList.add(choice);
        choice = new Choice("Kim Jong Un");
        Log.d("Item Count", String.valueOf(adapter.getItemCount()));
        choiceArrayList.add(choice);
    



【讨论】:

但这不起作用。没有片段视图,我的代码可以正常工作【参考方案2】:

为 LayoutManager 指定方向。

示例代码。

LinearLayoutManager manager = new LinearLayoutManager(this);
managersetOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(manager);
choiceRecycleView2.setAdapter( adapter );

【讨论】:

正如您在其他文章中评论的那样,您的 recyclerview 正在没有片段的情况下工作,然后做一件事检查 recycler 视图是否显示。从基础开始。首先给recyclerview提供静态大小并给它的背景颜色。看看它的显示与否。 是的,它显示在预览中。但它完全“跳过”了布局,甚至不运行onCreateView。因此出现错误E/RecyclerView: No adapter attached; skipping layout【参考方案3】:

显然这是 AMD 处理器的问题。我无法证明这一点,因为我被那门课踢了,我在 2 个月前完成了那门课

【讨论】:

以上是关于E/RecyclerView:没有附加适配器;跳过布局。尽管初始化适配器的主要内容,如果未能解决你的问题,请参考以下文章

Fragment,RecyclerView:没有附加适配器;跳过布局

recyclerView 没有适配器附加,跳过布局

Firebase:Recyclerview 没有附加适配器,跳过布局

Firebase:Recyclerview 没有附加适配器,跳过布局

Recyclerview 未连接适配器

尝试设置适配器:不断获取 E/RecyclerView:未连接适配器;跳过布局[重复]