“No adapter attached; skipping layout” on a fragment

痴心易碎 提交于 2019-12-25 01:23:15

问题


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);
}
}

Gives me

E/RecyclerView: No adapter attached; skipping layout

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.


回答1:


the problem is mostly this line

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

you should have

return rootView

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.



来源:https://stackoverflow.com/questions/36679542/no-adapter-attached-skipping-layout-on-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!