大家好!我在应用程序崩溃时遇到问题,它有一个错误
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(Unknown Source:6)
at com.example.jasper.happy_paws.CommentAdapter.getUserInfo(CommentAdapter.java:95)
at com.example.jasper.happy_paws.CommentAdapter.onBindViewHolder(CommentAdapter.java:51)
at com.example.jasper.happy_paws.CommentAdapter.onBindViewHolder(CommentAdapter.java:26)
at android.support.v7.widget.RecyclerView$Adapter
我的适配器没有任何问题,这是我的代码
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> {
private Context mContext;
private List<CommentModel>mComment;
private FirebaseUser firebaseUser;
public CommentAdapter(Context mContext, List<CommentModel> mComment) {
this.mContext = mContext;
this.mComment = mComment;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.comment_items, parent, false);
return new CommentAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final CommentModel commentModel = mComment.get(i);
holder.comment.setText(commentModel.getComment());
getUserInfo(holder.image_profile,holder.username, commentModel.getUserid());
holder.comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, Account.class);
intent.putExtra("userid", commentModel.getUserid());
mContext.startActivity(intent);
}
});
holder.image_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, Account.class);
intent.putExtra("userid", commentModel.getUserid());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mComment.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public ImageView image_profile;
public TextView username,comment;
public ViewHolder(View itemView) {
super(itemView);
image_profile = itemView.findViewById(R.id.image_profile);
username = itemView.findViewById(R.id.username);
comment = itemView.findViewById(R.id.comment);
}
}
private void getUserInfo(final ImageView imageView, final TextView username, String userid)
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("User").child(userid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
UsersHappyPaws usersHappyPaws = dataSnapshot.getValue(UsersHappyPaws.class);
Glide.with(mContext).load(usersHappyPaws.getProfilepic()).into(imageView);
username.setText(usersHappyPaws.getName());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:0)
尝试这种方式。 如果获取数据代码有效,请更改这些内容。
从firebase获取数据会涉及活动或碎片。
private List<UsersHappyPaws> pawsList=new ArrayList<>();
private void getUserInfo(final ImageView imageView, final TextView username, String userid)
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("User").child(userid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
UsersHappyPaws usersHappyPaws = dataSnapshot.getValue(UsersHappyPaws.class);
pawsList.add(usersHappyPaws);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
make适配器类make构造函数将传递数据添加到mange中。将一个数组列表放入适配器类中。
public CommentAdapter(Context mContext, List<CommentModel> mComment,List<UsersHappyPaws> pawsList) {
this.mContext = mContext;
this.mComment = mComment;
this.pawsList=pawsList;
}
之后,为UsersHappyPaws类创建对象并根据其绑定数据。