我想使用Firebase回收适配器显示所有用户的帖子。但是我遇到Firebase数据库异常错误并且我的应用程序崩溃了。我不知道我做错了什么?
我最初做得很好,我的应用程序运行得很好。然后我不知道我做错了什么,但现在我被卡住了,不知道如何解决这个问题。就我认为我在其他一些活动中进行了更改,但后来我撤消了它。但我不明白帖子中是如何发生错误的,因为我没有在这里进行任何更改,它以前运行得很好。
这是我的数据库参考:
postRef= FirebaseDatabase.getInstance().getReference().child("Posts");
这是我的Firebase Recycler适配器代码:
FirebaseRecyclerOptions<Posts> options=new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(postRef,Posts.class)
.build();
FirebaseRecyclerAdapter<Posts,PostsViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Posts, PostsViewHolder>(options)
{
@Override
protected void onBindViewHolder(@NonNull final PostsViewHolder holder, int position, @NonNull final Posts model) {
final String postKey=getRef(position).getKey();
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent clickPostIntent=new Intent(MainActivity.this, ClickPostActivity.class);
clickPostIntent.putExtra("postKey",postKey);
startActivity(clickPostIntent);
}
});
holder.username.setText(model.getFullname());
holder.date.setText(model.getDate());
holder.time.setText(model.getTime());
if (model.getPost_uri_link()!=null){
holder.post_link.setText(model.getPost_uri_link());
}
else {
holder.post_link.setVisibility(View.GONE);
}
if (model.getPost_file_name()!=null && model.getPost_file_uri()!=null) {
holder.file_attach_name.setText(model.getPost_file_name());
holder.file_attach_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadFile(MainActivity.this,model.getPost_file_name(),"gs://studentcornerproject-be4c6.appspot.com/post files",model.getPost_file_uri());
}
});
}
else{
holder.file_attach_name.setVisibility(View.GONE);
}
if (model.getDescription()!=null) {
holder.description.setText(model.getDescription());
}
else {
holder.description.setVisibility(View.GONE);
}
if (model.getPost_img()!=null) {
Picasso.get().load(model.post_img).networkPolicy(NetworkPolicy.OFFLINE).into(holder.post_img, new Callback() {
@Override
public void onSuccess() {
postRef.child("post_img").keepSynced(true);
}
@Override
public void onError(Exception e) {
Picasso.get().load(model.post_img).into(holder.post_img);
}
});
}
else {
holder.post_img.setVisibility(View.GONE);
}
Picasso.get().load(model.profile_image).networkPolicy(NetworkPolicy.OFFLINE).into(holder.prof_img, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Picasso.get().load(model.profile_image).into(holder.prof_img);
}
});
}
@NonNull
@Override
public PostsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.all_posts_users,viewGroup,false);
PostsViewHolder viewHolder=new PostsViewHolder(view);
return viewHolder;
}
};
postslist.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
public static class PostsViewHolder extends RecyclerView.ViewHolder
{
TextView username,date,time,description,file_attach_name,post_link;
CircleImageView prof_img;
ImageView post_img;
public PostsViewHolder(@NonNull View itemView) {
super(itemView);
post_link=itemView.findViewById(R.id.post_link);
username=itemView.findViewById(R.id.post_user_name);
date=itemView.findViewById(R.id.post_date);
time=itemView.findViewById(R.id.post_time);
description=itemView.findViewById(R.id.post_description);
file_attach_name=itemView.findViewById(R.id.post_filename);
prof_img=itemView.findViewById(R.id.post_profile_img);
post_img=itemView.findViewById(R.id.post_image);
}
}
这是我的Post. class:
public String uid, date, time, description, fullname, profile_image, post_img, post_file_uri, post_file_name,post_uri_link;
//default constructor
public Posts() {
}
public Posts(String uid, String date, String time, String description, String fullname, String profile_image,String post_uri_link, String post_img, String post_file_uri, String post_file_name) {
this.uid = uid;
this.date = date;
this.time = time;
this.description = description;
this.fullname = fullname;
this.profile_image = profile_image;
this.post_img = post_img;
this.post_file_uri = post_file_uri;
this.post_file_name = post_file_name;
this.post_uri_link=post_uri_link;
}
public String getPost_uri_link() {
return post_uri_link;
}
public void setPost_uri_link(String post_uri_link) {
this.post_uri_link = post_uri_link;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getProfile_image() {
return profile_image;
}
public void setProfile_image(String profile_image) {
this.profile_image = profile_image;
}
public String getPost_img() {
return post_img;
}
public void setPost_img(String post_img) {
this.post_img = post_img;
}
public String getPost_file_uri() {
return post_file_uri;
}
public void setPost_file_uri(String post_file_uri) {
this.post_file_uri = post_file_uri;
}
public String getPost_file_name() {
return post_file_name;
}
public void setPost_file_name(String post_file_name) {
this.post_file_name = post_file_name;
}
我收到以下错误:
我的问题已经解决了。有数据库引用问题。现在它工作正常。