我想在片段内显示recycleler视图,但我得到 java.lang.NullPointerException:尝试在null上调用虚方法'boolean android.support.v7.widget.RecyclerView $ LayoutManager.canScrollVertically()'对象参考。。代码如下。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
ArrayList<String> values = getArguments().getStringArrayList("arrayList");
RecyclerViewAdapter cl = new RecyclerViewAdapter(ctx, values);
recyclerView.setAdapter(cl);
return inflater.inflate(R.layout.fragment_blank, container, false);
}
答案 0 :(得分:1)
尝试使用:
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
我认为你的上下文没有在构造linearlayoutmanager
中传递答案 1 :(得分:0)
您正在创建一个视图(在第一行中),您忽略它并返回其他内容。
你应该这样做:
return(rootview)