com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.studentcornerapp.studentcorner.Posts.Posts

时间:2019-05-22 21:22:20

标签: java android database firebase exception

我想显示所有使用Firebase Recycler适配器的用户帖子。但是出现Firebase Database Exception错误,我的应用程序崩溃了。我不知道我在做什么错?

最初,我做的一切正确,我的应用程序运行完美。然后我不知道我做错了什么,但现在我被困住了,也不知道如何解决这个问题。到目前为止,我认为我正在其他活动中进行更改,但是后来我撤消了。但是我不了解Posts中如何发生错误,因为我在这里没有进行任何更改,并且之前运行得很好。

这是我的数据库参考:

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);


        }
    }

这是我的Posts.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;
    }

我遇到以下错误:

  

2019-05-23 01:41:13.708 26524-26524 / com.studentcornerapp.studentcorner E / Android运行时:致命异常:主要       流程:com.studentcornerapp.studentcorner,PID:26524       com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.studentcornerapp.studentcorner.Posts.Posts类型           在com.google.android.gms.internal.firebase_database.zzkt.zzb处(未知来源:209)           位于com.google.android.gms.internal.firebase_database.zzkt.zza(未知来源:0)           位于com.google.firebase.database.DataSnapshot.getValue(未知来源:10)           在com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)           在com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)           在com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)           在com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)           在com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:106)           在com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)           在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6781)           在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6823)           在android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)           在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5858)           在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5854)           在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2230)           在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)           在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)           在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)           在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)           在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)           在android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888)           在android.support.v7.widget.RecyclerView $ 1.run(RecyclerView.java:407)           在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:981)           在android.view.Choreographer.doCallbacks(Choreographer.java:790)           在android.view.Choreographer.doFrame(Choreographer.java:718)           在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:967)           在android.os.Handler.handleCallback(Handler.java:808)           在android.os.Handler.dispatchMessage(Handler.java:101)           在android.os.Looper.loop(Looper.java:166)           在android.app.ActivityThread.main(ActivityThread.java:7529)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:245)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

1 个答案:

答案 0 :(得分:0)

我的问题已解决。没有数据库参考问题。现在一切正常。