【问题标题】:"No adapter attached; skipping layout" on a fragment片段上的“未连接适配器;跳过布局”
【发布时间】:2016-04-17 17:03:19
【问题描述】:
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

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

【问题讨论】:

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


    【解决方案1】:

    问题主要是这一行

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

    你应该有

    return rootView

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

    【讨论】:

    • 最糟糕的错误隐藏在显而易见的地方。谢谢!
    猜你喜欢
    • 2021-05-28
    • 2021-08-20
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2021-04-24
    • 2020-03-12
    相关资源
    最近更新 更多