您能在我的代码中看到问题吗?我得到了这个例外:
java.lang.NullPointerException:
Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
使用此代码:
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.latest_events_fragment, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.events_recycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager );
list.add("A");
list.add("B");
list.add("C");;
// specify an adapter
adapter = new EventsRecyclerAdapter(list);
recyclerView.setAdapter(adapter);
return view;
}
这是我的.xml文件,它被称为events_list.xml `
`
答案 0 :(得分:1)
您的回收商视图对象为空。尝试在此处找到它时,请确保使用正确的ID:
recyclerView = (RecyclerView)view.findViewById(R.id.events_recycler);
如果ID正确,请确保使用所需的布局。