gpt4 book ai didi

java - 适配器类出现问题,没有崩溃,但页面卡住并重新加载

转载 作者:行者123 更新时间:2023-12-01 18:04:39 25 4
gpt4 key购买 nike

在我的 CommentAdapter.java 类中,我有以下代码。当用户对某个帖子发表评论时,图像图标必须从黑色聊天图标更改为黄色聊天图标。工作正常。当用户删除他们在帖子上留下的评论时,就会出现问题。如果他们删除了自己的评论,并且该用户对该帖子没有其他评论,则聊天图标应从黄色更改回黑色。

它可以工作,我得到了想要的结果,但由于某种原因它抛出了一个空指针,页面卡住,重新加载,然后它带我回到带有期望结果的主页。我需要整个过程运行得更流畅,显然没有任何空指针。

是的,显然我知道空指针是什么以及如何修复它们,但在这里我不明白为什么我会得到它。聊天图标已存在。

下方用箭头标记的崩溃线。

评论适配器

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> {

ImageView mImageView;

private Context mContext;
private List<Comment> mComment;
private String postid;
private FirebaseUser mFirebaseUser;

public CommentAdapter(Context mContext, List<Comment> mComment, String postid) {
this.mContext = mContext;
this.mComment = mComment;
this.postid = postid;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.comment_item, parent, false);
return new CommentAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final Comment comment = mComment.get(position);

holder.comment.setText(comment.getComment());
getUserInfo(holder.image_profile, holder.username, comment.getPublisher());

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
if (comment.getPublisher().equals(mFirebaseUser.getUid())) {

AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
alertDialog.setTitle("Do you want to delete this comment?");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("POSTID ->", postid);
FirebaseDatabase.getInstance().getReference("Comments").child(postid).child(comment.getCommentid())
.setValue(null).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
-------------------------------------------> mImageView.setImageResource(R.drawable.ic_chat_black);
Toast.makeText(mContext, "Your comment has been deleted", Toast.LENGTH_SHORT).show();
}
}
});
dialog.dismiss();
}
});
alertDialog.show();
}
return true;
}
});

public static class ViewHolder extends RecyclerView.ViewHolder {

ImageView image_profile, imageView;
public TextView username, comment;

ViewHolder(@NonNull View itemView) {
super(itemView);

image_profile = itemView.findViewById(R.id.image_profile);
comment = itemView.findViewById(R.id.comment);
username = itemView.findViewById(R.id.username);
imageView = itemView.findViewById(R.id.commentPost);
}
}

Logcat

E/AndroidRuntime:致命异常:main 进程:com.e.events,PID:18463 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ImageView.setImageResource(int)” 在 com.e.events.Adapter.CommentAdapter$3$2$1.onComplete(CommentAdapter.java:110) 在 com.google.android.gms.tasks.zzj.run(来源未知:4) 在 android.os.Handler.handleCallback(Handler.java:907) 在 android.os.Handler.dispatchMessage(Handler.java:105) 在 android.os.Looper.loop(Looper.java:216) 在 android.app.ActivityThread.main(ActivityThread.java:7625) 在 java.lang.reflect.Method.invoke( native 方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

最佳答案

如果您的 Comment 类是自定义类,我建议将图像资源值存储在 Comment 类本身中,然后在更改 Comment 的图像值时刷新适配器。它应该看起来像这样:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position){
// ...

final Comment comment = mComment.get(position);

// Use a getter you defined in your Comment class to set the ViewHolder's image
// (Make sure your ViewHolder class has an ImageView in it for this to work)
// *(Also, make sure your Comment class already has a default Image Resource
// value assigned within it)
holder.myImageView.setImageResource(comment.getImgResource());

// ...
}

然后在回调中,您之前设置图像资源的位置:

if(task.isSuccessful){
// Update the image resource stored in the Comment
comment.setImgResource(myNewImageResource);

// Anything else you want to do ...

notifyDataSetChanged(); // Notify the adapter to refresh
}

关于java - 适配器类出现问题,没有崩溃,但页面卡住并重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60578266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com