我尝试了很多方法来阻止崩溃,但都无济于事,非常感谢您的帮助。这是Logcat的消息。
Process: com.niftyware.i_gbadunblog, PID: 16919 java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database@@19.2.1:96) at com.google.firebase.database. DataSnapshot.child(com.google.firebase:firebase-database@@19.2.1:65)在com.niftyware.i_gbadunblog.Adapters.CommentsAdapter$CommentViewHolder$1.onDataChange(CommentsAdapter.java:210)处。 google.firebase.数据库.核心.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75)在com.google.firebase.数据库.核心.view.DataEvent.fire(com.google. firebase:firebase-database@@19.2.1:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55) at android.os.Handler.handleCallback(Handler.java:883)
这是Comments Adapter的代码,直到该行的错误部分。
@Override
public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
final String CommentKey = mData.get(position).getCommentKey();
Glide.with(mCtx).load(mData.get(position).getUimg()).into(holder.img_user);
holder.txt_name.setText(mData.get(position).getUname());
holder.txt_content.setText(mData.get(position).getContent());
holder.txt_date.setText(timestampToString((Long) mData.get(position).getTimestamp()));
holder.reply.setText(mData.get(position).getReply());
holder.setLikeCommentBtnStatus(CommentKey);
holder.setUnlikeCommentBtnStatus(CommentKey);
holder.setReplyCommentBtnStatus();
holder.btnLikeComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Here I would add a code of a variable which would check the number of likes and to change the color of the liked post
likesChecker = true;
// User can likes a single post one time only 1 count
likesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(likesChecker.equals(true)){
if (dataSnapshot.child(CommentKey).hasChild(currentUserIDX)){// if likes exists
likesRef.child(CommentKey).child(currentUserIDX).removeValue();
likesChecker = false;
}
else{
likesRef.child(CommentKey).child(currentUserIDX).setValue(true);
likesChecker = false;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
holder.btnDislikeComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dislikesChecker = false;
disLikesRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dislikesChecker.equals(true)){
if(dataSnapshot.child(CommentKey).hasChild(currentUserIDX)){
disLikesRef.child(CommentKey).child(currentUserIDX).removeValue();
dislikesChecker = false;
}
else{
disLikesRef.child(CommentKey).child(currentUserIDX).setValue(true);
dislikesChecker = false;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public class CommentViewHolder extends RecyclerView.ViewHolder {
ImageView img_user;
TextView txt_name, txt_content, txt_date;
ImageButton btnLikeComment, btnDislikeComment;
TextView displayNoOfLikes, displayNoOfDislikes, reply;
int countLikes;
int countDislikes;
String currentUserID;
DatabaseReference LikeRef, DislikeRef, ReplyRef;
public CommentViewHolder(View itemView){
super(itemView);
img_user = itemView.findViewById(R.id.comment_user_img);
txt_name = itemView.findViewById(R.id.comment_username);
txt_content = itemView.findViewById(R.id.comment_content);
txt_date = itemView.findViewById(R.id.comment_date);
LikeRef = FirebaseDatabase.getInstance().getReference().child("Likes");
DislikeRef = FirebaseDatabase.getInstance().getReference().child("Dislikes");
ReplyRef = FirebaseDatabase.getInstance().getReference().child("Replies");
btnLikeComment = itemView.findViewById(R.id.comment_like_post);
btnLikeComment.setBackgroundColor(Color.TRANSPARENT);
btnDislikeComment = itemView.findViewById(R.id.comment_dislike_post);
btnDislikeComment.setBackgroundColor(Color.TRANSPARENT);
displayNoOfLikes = itemView.findViewById(R.id.comment_like_post_display_no_of_likes);
displayNoOfLikes = itemView.findViewById(R.id.comment_like_post_display_no_of_dislikes);
reply = itemView.findViewById(R.id.comment_reply);
}
public void setLikeCommentBtnStatus(final String CommentKey){
LikeRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.child(CommentKey).hasChild(currentUserID)){
countLikes = (int) dataSnapshot.child(CommentKey).getChildrenCount(); //Counts the number of likes in a single post
displayNoOfLikes.setText((Integer.toString(countLikes) + (" Likes")));
}
else{
// If the user unlikes the post I have to update the number of likes and change the color of the like
countLikes = (int) dataSnapshot.child(CommentKey).getChildrenCount(); //Counts the number of likes in a single post
displayNoOfLikes.setText((Integer.toString(countLikes) + (" Likes")));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
伙计们,我在setLikeBtnCommentStatus方法上找到了解决方法,错误来自于包含在if语句中的Comment键。我只是修改了
if(dataSnapshot.child(CommentKey).hasChild(currentUserID)){ countLikes = (int) dataSnapshot.child(CommentKey).getChildrenCount(); /计算单个帖子中的赞数 displayNoOfLikes.setText((Integer.toString(countLikes)+("Likes")); }。
Toif(dataSnapshot.child(String.valueOf(CommentKey)).hasChild(currentUserID)){ countLikes = (int) dataSnapshot.child(CommentKey).getChildrenCount(); /计算单个帖子中的赞数 displayNoOfLikes.setText((Integer.toString(countLikes)+("赞")); }这解决了我的问题。