原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView $ Adapter)” 在com.datbrothers.califinder.MainActivity.onStart(MainActivity.java:379) 在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1340) 在android.app.Activity.performStart(Activity.java:7200) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2920) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 在android.app.ActivityThread.-wrap11(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1696) 在android.os.Handler.dispatchMessage(Handler.java:105) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6944) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Comments> options = new FirebaseRecyclerOptions.Builder<Comments>().setQuery(MarkersRef, Comments.class).build();
FirebaseRecyclerAdapter<Comments, CommentsViewHolder> adapter = new FirebaseRecyclerAdapter<Comments, CommentsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull CommentsViewHolder holder, int position, @NonNull Comments model) {
holder.username.setText(model.getUsername());
holder.comment.setText(model.getComment());
holder.date.setText(model.getDate());
holder.time.setText(model.getTime());
}
@NonNull
@Override
public CommentsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.comment, viewGroup, false);
CommentsViewHolder viewHolder = new CommentsViewHolder(view);
return viewHolder;
}
};
CommentsList.setAdapter(adapter);
adapter.startListening();
}
public static class CommentsViewHolder extends RecyclerView.ViewHolder {
TextView comment, date, time, username;
public CommentsViewHolder(@NonNull View itemView) {
super(itemView);
username = itemView.findViewById(R.id.username_comment);
comment = itemView.findViewById(R.id.comment_text);
date = itemView.findViewById(R.id.date_comment);
time = itemView.findViewById(R.id.time_comment);
}
}
和评论列表
MarkersRef = FirebaseDatabase.getInstance().getReference("Users").child(marker.getTitle().toString()).child("Comments");
CommentsList = (RecyclerView) myDialog.findViewById(R.id.comments_list);
CommentsList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
CommentsList.setLayoutManager(linearLayoutManager);
请问我真的需要您的帮助