"No adapter attached; skipping layout" 在片段上

"No adapter attached; skipping layout" on a fragment

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

我已经尝试了我找到的所有解决方案(设置一个空适配器、将代码移到别处、使用单独的线程)但无济于事。这应该可以正常工作 activity 所以我想也许我做错了什么。

主要是这一行的问题

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

你应该

return rootView

对于第一个 return,您正在膨胀一个完全不同的新视图层次结构,从 fragment_work.xml 开始,从 RecyclerView 正确设置的视图层次结构开始。