有时,我总是遇到应用崩溃的情况,但并不总是使用以下代码,这对我来说很奇怪。我已经尝试过在该主题上找到的所有内容,包括摆脱for循环(第130行),(应用程序崩溃表明第131行是问题)。comment comment = snapshot.getValue(Comment.class);。真的不知道该如何解决。在Firebase中,我有一个父类“ Comments”和两个孩子“ comment”和“ publisher”。如果有人有任何见识,将不胜感激。
CommentsActivity
package com.e.events;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.e.events.Adapter.CommentAdapter;
import com.e.events.Model.Comment;
import com.e.events.Model.User;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class CommentsActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private CommentAdapter commentAdapter;
private List<Comment> commentList;
EditText addcomment;
ImageView image_profile, checkmark_post_comment;
String postid;
String publisherid;
FirebaseUser firebaseUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Comments");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
commentList = new ArrayList<>();
commentAdapter = new CommentAdapter(this, commentList);
recyclerView.setAdapter(commentAdapter);
addcomment = findViewById(R.id.add_comment);
image_profile = findViewById(R.id.image_profile);
checkmark_post_comment = findViewById(R.id.checkmark_post_comment);
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
Intent intent = getIntent();
postid = intent.getStringExtra("postid");
publisherid = intent.getStringExtra("publisherid");
checkmark_post_comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (addcomment.getText().toString().equals("")) {
Toast.makeText(CommentsActivity.this, "Cannot send empty comments", Toast.LENGTH_SHORT).show();
} else {
addcomment();
}
}
});
getImage();
readComments();
}
private void addcomment() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments").child(postid);
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("comment", addcomment.getText().toString());
hashMap.put("publisher", firebaseUser.getUid());
reference.push().setValue(hashMap);
addcomment.setText("");
}
private void getImage() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Glide.with(getApplicationContext()).load(user.getImageurl()).into(image_profile);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void readComments() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments").child(postid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
commentList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Comment comment = snapshot.getValue(Comment.class);
commentList.add(comment);
}
commentAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
LOGCAT
进程:com.e.events,PID:741 com.google.firebase.database.DatabaseException:无法将java.lang.Boolean类型的对象转换为com.e.events.Model.Comment类型 在com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database @@ 19.2.0:435) com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database @@ 19.2.0:231) com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database @@ 19.2.0:79) com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database @@ 19.2.0:203) 在com.e.events.CommentsActivity $ 4.onDataChange(CommentsActivity.java:131) com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database @@ 19.2.0:75) com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database @@ 19.2.0:63) com.google.firebase.database.core.view.EventRaiser $ 1.run(com.google.firebase:firebase-database @@ 19.2.0:55) 在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(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:524) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
CommentAdapter
package com.e.events.Adapter;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.e.events.MainActivity;
import com.e.events.Model.Comment;
import com.e.events.Model.User;
import com.e.events.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.List;
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder>{
private Context mContext;
private List<Comment> mComment;
private FirebaseUser firebaseUser;
public CommentAdapter(Context mContext, List<Comment> 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_item, parent, false);
return new CommentAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final Comment comment = mComment.get(position);
holder.comment.setText(comment.getComment());
getUserInfo(holder.image_profile, holder.username, comment.getPublisher());
holder.comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, MainActivity.class);
intent.putExtra("publisherid", comment.getComment());
mContext.startActivity(intent);
}
});
holder.image_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, MainActivity.class);
intent.putExtra("publisherid", comment.getComment());
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
if (mComment != null) {
return mComment.size();
} else {
return 0;
}
}
public class ViewHolder extends RecyclerView.ViewHolder{
public ImageView image_profile;
public TextView username, comment;
public 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);
}
}
private void getUserInfo(final ImageView imageView, final TextView username, String publisherid) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child(publisherid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Glide.with(mContext).load(user.getImageurl()).into(imageView);
username.setText(user.getUsername());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
Model Comments
package com.e.events.Model;
public class Comment {
private String comment;
private String publisher;
public Comment(String comment, String publisher) {
this.comment = comment;
this.publisher = publisher;
}
public Comment() {
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}