"没有连接适配器;跳过布局"在片段上

时间:2016-04-17 17:03:19

标签: android android-fragments android-recyclerview android-adapter

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

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

1 个答案:

答案 0 :(得分:1)

问题主要是这一行

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

return rootView

在第一次返回时,您正在从fragment_work.xml开始,从正确设置RecyclerView的视图层次结构中膨胀一个完全不同的视图层次结构。