java在RecyclerView布局管理器上获取空指针异常
嗨,伙计们,我有以下错误,我似乎无法解决
我有
java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.support.v7.widget.RecyclerView.setLayoutManager(安卓.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
我的代码与常规示例略有不同,因为我是在片段中而不是在活动中执行此操作的
这是我的密码
我有一个完整片段的xml:
<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="mtr.MainActivityFragmentTwo">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/grades"
安卓:textStyle="bold"
安卓:textSize="40dp"
安卓:gravity="center_horizontal"
安卓:layout_gravity="center"
/>
</FrameLayout>
然后我有一个content_片段,它保存着RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<安卓.support.v7.widget.RecyclerView
安卓:id="@+id/realm_recycler_view"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" />
</FrameLayout>
然后,片段中的代码如下所示
public class MainActivityFragmentTwo extends Fragment {
private Realm realm;
private RecyclerView recyclerView;
public MainActivityFragmentTwo() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main_activity_fragment_two, container, false);
}
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
realm.init(getActivity());
realm = Realm.getDefaultInstance();
recyclerView = (RecyclerView) getActivity().findViewById(R.id.realm_recycler_view);
setupRecyclerView();
}
public void setupRecyclerView() {
Log.d("Found grades", "not showing grades tho");
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
recyclerView.setAdapter(new gradeRealmAdapter(this, realm.where(Grade.class).findAllAsync()));
recyclerView.setHasFixedSize(true);
}
}
我创建的适配器是
public class gradeRealmAdapter extends RealmRecyclerViewAdapter<Grade, gradeRealmAdapter.GradeViewHolder> {
private final MainActivityFragmentTwo activity;
public gradeRealmAdapter (MainActivityFragmentTwo activity, RealmResults<Grade> realmResults) {
super(activity.getActivity(), realmResults, true);
this.activity = activity;
}
@Override
public GradeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new GradeViewHolder(inflater.inflate(R.layout.gradeitem, parent, false));
}
@Override
public void onBindViewHolder(GradeViewHolder holder, int position) {
Grade grade = getData().get(position);
holder.title.setText(grade.toString());
}
class GradeViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
public TextView title;
public Grade grade;
public GradeViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.grade_text_view);
view.setOnLongClickListener(this);
}
public boolean onLongClick(View v) {
activity.deleteItem(grade);
return true;
}
}
}
错误来自下一行的Fragment类
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
我似乎无法解决这个问题
有人能看出我做错了什么吗
# 1 楼答案
然后,您需要
findViewById
在布局上,而不是活动的然后,您还可以向后进行
realm
初始化因此,在
onAttach
中也要解决这个问题,在那里你保证会有一些Context
# 2 楼答案
在
onViewCreated()
上放大回收器视图不在
onCreate
# 3 楼答案
让我们试着改变
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
与
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
并对onCreateView进行一些更新: