“未连接适配器;跳过布局"在片段上 [英] "No adapter attached; skipping layout" on a fragment

查看:134
本文介绍了“未连接适配器;跳过布局"在片段上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class WorkFragment extends Fragment {

List<CardViewItem> items = new ArrayList<>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("FRAGMENT", "Work Fragment started");

    TypedArray icons = getResources().obtainTypedArray(R.array.project_icons);
    TypedArray names = getResources().obtainTypedArray(R.array.project_names);
    TypedArray descs = getResources().obtainTypedArray(R.array.project_descs);
    for (int i=0;i<icons.length();i++){
        items.add(new CardViewItem(icons.getDrawable(i),
                names.getString(i),
                descs.getString(i)));
    }
    icons.recycle();
    names.recycle();
    descs.recycle();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d("FRAGMENT", "Work Fragment onCreateView");
    View rootView = inflater.inflate(R.layout.fragment_work, container, false);
    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(llm);
    recyclerView.setAdapter(new ItemAdapter(items));
    return inflater.inflate(R.layout.fragment_work, container, false);
}
}

给我

E/RecyclerView: No adapter attached; skipping layout

我已经尝试了所有找到的解决方案(设置一个空适配器,将代码els地方移动,使用单独的线程),但无济于事.这应该可以正常进行,所以我想我可能做错了事.

I have tried out all the solutions I had found (setting an empty adapter, moving the code elswhere, using a seperate thread) but to no avail. This should work on a normal activity so I guess maybe I'm doing something wrong.

推荐答案

问题主要是此行

return inflater.inflate(R.layout.fragment_work, container, false);

您应该拥有

return rootView

有了第一个返回项,您将从一个正确设置RecyclerView的视图层次扩展一个新的完全不同的视图层次结构,从fragment_work.xml开始.

With the first return you are inflating a new totally different view hierarchy, starting from fragment_work.xml, from the one which has an the RecyclerView correctly set up.

这篇关于“未连接适配器;跳过布局"在片段上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆