空指针异常

w8biq8rn  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(456)

我正在尝试填充 recyclerView 使用firestore的java对象。recyclerview位于片段中。我用的是一个循环水适配器,它延长了 FirestoreRecyclerAdapter 班级。当我运行代码时,应用程序立即崩溃。
这是错误。

.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference

以下是一些片段java代码:

public class EventsFragment extends Fragment{
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference eventRef = db.collection("Events");

    private EventRecyclerAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.events_fragment, container, false);

        setUpRecyclerView();
        return rootView;
    }

    private void setUpRecyclerView(){
        Query query = eventRef.limit(8);
        FirestoreRecyclerOptions<Event> options = new FirestoreRecyclerOptions.Builder<Event>()
                .setQuery(query, Event.class)
                .build();

        adapter = new EventRecyclerAdapter(options);

        RecyclerView recyclerView = (RecyclerView) getActivity().findViewById(R.id.events_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);
    }

 }

以下是xml片段:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.MaterialToolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="Upcoming Events"
            app:menu="@menu/main_menu"
            android:background="@android:color/white"
            />

    </com.google.android.material.appbar.AppBarLayout>

        <androidx.recyclerview.widget.RecyclerView
            app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="8dp"
            android:clipToPadding="false"
            android:divider="@null"
            android:id="@+id/events_recycler_view"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

以下是recycleradapter代码:

public class EventRecyclerAdapter extends FirestoreRecyclerAdapter<Event, EventRecyclerAdapter.EventHolder> {

    public EventRecyclerAdapter(@NonNull FirestoreRecyclerOptions<Event> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull EventHolder holder, int position, @NonNull Event model) {
        holder.textViewTitle.setText(model.getTitle());
        holder.textViewDescription.setText(model.getDescription());
    }

    @NonNull
    @Override
    public EventHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.events_list_item,
                parent, false);
        return new EventHolder(v);
    }

    class EventHolder extends RecyclerView.ViewHolder {
        TextView textViewTitle;
        TextView textViewDescription;

        public EventHolder(@NonNull View itemView) {
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.title);
            textViewDescription = itemView.findViewById(R.id.description);
        }
    }
}

我相信问题出在回收站的某个地方。我已经试着找这个问题好几个小时了。有什么我忽略了吗?我只需要它跑起来。

c9x0cxw0

c9x0cxw01#

你已经注意到了 recyclerViewnull 当你试图设置 LayoutManagersetUpRecyclerView() 发生这种情况是因为您正试图“找到” RecyclerViewActivityView 树在树的前面 FragmentView 实际上是附属于它的:
recyclerview recyclerview=(recyclerview)getactivity().findviewbyid(r.id.events\u recycler\u view);
第一个 Fragment 可以放置此语句的生命周期方法是 onResume() (至少在我运行android9的模拟器上的示例应用程序中是这样)
但是您可以配置 RecyclerView 因为你可以在 Fragment 他自己的 View 例如在 onViewCreated() :

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.events_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext());
    // etc.
}

请注意,中的view参数 onViewCreated() 表示 View 在世界上任何地方都可以买到 Fragment 通过呼叫 getView() . 但只有在 onCreateView() 已完成,因为此方法的目的是创建和“发布” FragmentView 首先。
最后同样重要的是,在 onCreateView() 你可以找到 RecyclerView 通过新膨胀的 View :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.events_fragment, container, false);

    //setUpRecyclerView();
    RecyclerView recyclerView = (RecyclerView) rootView .findViewById(R.id.events_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(rootView.getContext());
    // etc.

    return rootView;
}

所以也许你最好把根传过去 View 作为参数输入 setUpRecyclerView() 使用方法如下:

private void setUpRecyclerView(ViewGroup root){
    Query query = eventRef.limit(8);
    FirestoreRecyclerOptions<Event> options = new FirestoreRecyclerOptions.Builder<Event>()
            .setQuery(query, Event.class)
            .build();

    adapter = new EventRecyclerAdapter(options);

    RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.events_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(root.getContext()));
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(adapter);
}

相关问题