片段中的recyclerview的“RecyclerView:没有附加适配器;跳过布局”[重复]
Posted
技术标签:
【中文标题】片段中的recyclerview的“RecyclerView:没有附加适配器;跳过布局”[重复]【英文标题】:"RecyclerView: No Adapter attached; skipping layout" for recyclerview in fragment [duplicate] 【发布时间】:2015-09-02 22:11:06 【问题描述】:我收到了这个错误,但我不知道是什么原因造成的......也许这与它在片段中而不是在活动本身中初始化的事实有关。编辑:包含 StatsAdapter 代码
public class StatsFragment extends Fragment
private RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private RecyclerView.Adapter mAdapter;
private String[] myDataset = new String[]"hello", "world", "yolo";
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_stats, parent, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.cardList);
mRecyclerView.setHasFixedSize(true);
mAdapter = new StatsAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
return v;
public class StatsAdapter extends RecyclerView.Adapter<StatsAdapter.ViewHolder>
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder
public TextView mTextView;
public ViewHolder(View v)
super(v);
mTextView = (TextView) v.findViewById(R.id.title);
public StatsAdapter(String[] myDataset)
mDataset = myDataset;
@Override
public StatsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_stats, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
@Override
public void onBindViewHolder(ViewHolder holder, int position)
holder.mTextView.setText(mDataset[position]);
@Override
public int getItemCount()
return mDataset.length;
【问题讨论】:
在onCreate
中创建适配器和“非视图”相关对象。然后将您的adapter
用作recyclerview
。
@Jared Burrows,尽管结果是正确的,但这并不会改变结果。
@McGuile 发布您的答案或纠正我的答案。
@hshanguan,当您的适配器尚未设置(或设置不正确)时,会出现此特定错误消息。检查您的 StatsAdapter 类是否有错误。如果您想让我们看一下,请发布代码。
感谢您的帮助!将我的 StatsAdapter 代码添加到帖子中
【参考方案1】:
将setLayoutManager
行移到setAdapter
之前
【讨论】:
【参考方案2】:在onCreate
中创建适配器和“非视图”相关对象。然后将您的适配器用于RecyclerView
。
此外,通过根据对象的使用和Activity
生命周期以明确的顺序启动对象来清理您的代码。
public class StatsFragment extends Fragment
private RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private StatsAdapter mAdapter; // was RecyclerView.Adapter mAdapter;
private String[] myDataset = new String[]"hello", "world", "yolo";
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
// 1.
mAdapter = new StatsAdapter(myDataset);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_stats, parent, false);
// 2.
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// 3.
mRecyclerView = (RecyclerView) v.findViewById(R.id.cardList);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return v;
【讨论】:
执行 mRecyclerView.setAdapter(mAdapter);追第三部?试过了,还是一样的错误 您看到我所有的更改和 cmets 了吗?只需复制并粘贴代码。 @Jared 您能否为“在 onCreate 中创建“非视图”相关对象”添加更多说明。为什么它与在 OnCreateView 中添加它不同?【参考方案3】:解决此问题的一种方法是将空适配器附加到 RecyclerView
void initializeRecyclerView()
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setAdapter(new SampleAdapter(getCurrentActivity()));
recyclerView.setLayoutManager(new LinearLayoutManager(getCurrentActivity()));
recyclerView.setHasFixedSize(true);
【讨论】:
如果 FirebaseRecyclerAdapter 中发生此错误怎么办?以上是关于片段中的recyclerview的“RecyclerView:没有附加适配器;跳过布局”[重复]的主要内容,如果未能解决你的问题,请参考以下文章
更改片段(子片段)时 RecyclerView 中的奇怪叠加
Android:RecyclerView 不显示片段中的列表项