【发布时间】:2016-07-04 06:42:18
【问题描述】:
我是 recyclerview 的新手,在 cardview 中使用它,有人可以告诉我如何初始化它吗?
我知道我必须以某种方式将它放在 onCreateView 中,而不是在 ViewCreated 中,我的代码:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_genres, container, false);
以及我在 Activity 上使用的回收器代码,但现在我想在片段中触发它
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
数据的踪迹:
JSON>get>DataModel>Adapter>Recycler>CardView
当我在 onCreatedView 中有它时,LogCat 说:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
【问题讨论】: