com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.heartapp.Member2 [duplicate]类型

uqcuzwp8  于 2022-11-25  发布在  Go
关注(0)|答案(1)|浏览(100)

在我从数据库中检索数据之前,一切都运行良好。我想知道问题出在哪里,以及如何解决这个问题。我真的需要帮助。错误似乎发生在History.java的第43行。非常感谢您的回复。
以下是Logcat的错误代码

FATAL EXCEPTION: main
Process: com.example.heartapp, PID: 31504
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.heartapp.Member2
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
    at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
    at com.example.heartapp.History$1.onDataChange(History.java:43)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
    at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
    at android.os.Handler.handleCallback(Handler.java:888)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:213)
    at android.app.ActivityThread.main(ActivityThread.java:8178)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
2022-11-15 20:38:26.810 31504-31504 Process                 com.example.heartapp                 I  Sending signal. PID: 31504 SIG: 9
2022-11-15 20:38:26.854  1476-1670  InputDispatcher         pid-1476                             E  channel 'd52c022 com.example.heartapp/com.example.heartapp.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
---------------------------- PROCESS ENDED (31504) for package com.example.heartapp ----------------------------
2022-11-15 20:38:26.859  1476-1670  InputDispatcher         pid-1476                             E  channel '5fc9a49 com.example.heartapp/com.example.heartapp.History (server)' ~ Channel is unrecoverably broken and will be disposed!
2022-11-15 20:38:26.881  1476-2965  WindowManager           pid-1476                             E  win=Window{d52c022 u0 com.example.heartapp/com.example.heartapp.MainActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true
2022-11-15 20:38:26.908  2286-15179 DollieAdapterService    pid-2286                             E  notifyActivityState pkg:com.example.heartapp/com.example.heartapp.History state:20 fg:false mUid:10127

这是我的History.java

package com.example.heartapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

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;

public class History extends AppCompatActivity {

    RecyclerView recyclerView;
    DatabaseReference databaseReference;
    MyAdapter adapter;
    ArrayList<Member2> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        String email=getIntent().getStringExtra("keyemail");

        recyclerView=findViewById(R.id.History);
        databaseReference=FirebaseDatabase.getInstance().getReference(email);
        list=new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter=new MyAdapter(this,list);
        recyclerView.setAdapter(adapter);

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for(DataSnapshot dataSnapshot:snapshot.getChildren()){
                    Member2 user = dataSnapshot.getValue(Member2.class);
                    list.add(user);
                }

                adapter.notifyDataSetChanged();

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });


    }
}

这是我的Memeber2.java

package com.example.heartapp;

public class Member2 {
    String age;
    String sex;
    String stp1;
    String stp2a;
    String stp2b;
    String stp2c;
    String stp2d;
    String stp2e;
    String stp2f;
    String stp2g;
    String stp2h;
    String stp2i;
    String stp3;
    String stp4a;
    String stp4b;
    String stp4c;
    String stp4d;
    String stp4e;
    String stp4f;
    String stp4g;
    String stp4h;
    String stp5;
    String result;
    String tips;

    public Member2() {
    }

    public String getage() {
        return age;
    }

    public String getsex() {
        return sex;
    }

    public String getstp1() {
        return stp1;
    }

    public String getstp2a() {
        return stp2a;
    }

    public String getstp2b() {
        return stp2b;
    }

    public String getstp2c() {
        return stp2c;
    }

    public String getstp2d() {
        return stp2d;
    }

    public String getstp2e() {
        return stp2e;
    }

    public String getstp2f() {
        return stp2f;
    }

    public String getstp2g() {
        return stp2g;
    }

    public String getstp2h() {
        return stp2h;
    }

    public String getstp2i() {
        return stp2i;
    }

    public String getstp3() {
        return stp3;
    }

    public String getstp4a() {
        return stp4a;
    }

    public String getstp4b() {
        return stp4b;
    }

    public String getstp4c() {
        return stp4c;
    }

    public String getstp4d() {
        return stp4d;
    }

    public String getstp4e() {
        return stp4e;
    }

    public String getstp4f() {
        return stp4f;
    }

    public String getstp4g() {
        return stp4g;
    }

    public String getstp4h() {
        return stp4h;
    }

    public String getstp5() {
        return stp5;
    }

    public String getresult() {
        return result;
    }

    public String gettips() {
        return tips;
    }
}

下面是MyAdapter.java的代码

package com.example.heartapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;

    ArrayList<Member2> list;

    public MyAdapter(Context context, ArrayList<Member2> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.items,parent,false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Member2 user = list.get(position);
        holder.age.setText(user.getage());
        holder.result.setText(user.getresult());
        holder.sex.setText(user.getsex());
        holder.stp1.setText(user.getstp1());
        holder.stp2a.setText(user.getstp2a());
        holder.stp2b.setText(user.getstp2b());
        holder.stp2c.setText(user.getstp2c());
        holder.stp2d.setText(user.getstp2d());
        holder.stp2e.setText(user.getstp2e());
        holder.stp2f.setText(user.getstp2f());
        holder.stp2g.setText(user.getstp2g());
        holder.stp2h.setText(user.getstp2h());
        holder.stp2i.setText(user.getstp2i());
        holder.stp3.setText(user.getstp3());
        holder.stp4a.setText(user.getstp4a());
        holder.stp4b.setText(user.getstp4b());
        holder.stp4c.setText(user.getstp4c());
        holder.stp4d.setText(user.getstp4d());
        holder.stp4e.setText(user.getstp4e());
        holder.stp4f.setText(user.getstp4f());
        holder.stp4g.setText(user.getstp4g());
        holder.stp4h.setText(user.getstp4h());
        holder.stp5.setText(user.getstp5());
        holder.tips.setText(user.gettips());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView age, result, sex, stp1, stp2a, stp2b, stp2c, stp2d, stp2e, stp2f, stp2g, stp2h, stp2i, stp3, stp4a, stp4b, stp4c, stp4d, stp4e, stp4f, stp4g, stp4h, stp5, tips;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            age=itemView.findViewById(R.id.tvAge);
            result=itemView.findViewById(R.id.tvResult);
            sex=itemView.findViewById(R.id.tvSex);
            stp1=itemView.findViewById(R.id.tvstp1);
            stp2a=itemView.findViewById(R.id.tvstp2a);
            stp2b=itemView.findViewById(R.id.tvstp2b);
            stp2c=itemView.findViewById(R.id.tvstp2c);
            stp2d=itemView.findViewById(R.id.tvstp2d);
            stp2e=itemView.findViewById(R.id.tvstp2e);
            stp2f=itemView.findViewById(R.id.tvstp2f);
            stp2g=itemView.findViewById(R.id.tvstp2g);
            stp2h=itemView.findViewById(R.id.tvstp2h);
            stp2i=itemView.findViewById(R.id.tvstp2i);
            stp3=itemView.findViewById(R.id.tvstp3);
            stp4a=itemView.findViewById(R.id.tvstp4a);
            stp4b=itemView.findViewById(R.id.tvstp4b);
            stp4c=itemView.findViewById(R.id.tvstp4c);
            stp4d=itemView.findViewById(R.id.tvstp4d);
            stp4e=itemView.findViewById(R.id.tvstp4e);
            stp4f=itemView.findViewById(R.id.tvstp4f);
            stp4g=itemView.findViewById(R.id.tvstp4g);
            stp4h=itemView.findViewById(R.id.tvstp4h);
            stp5=itemView.findViewById(R.id.tvstp5);
            tips=itemView.findViewById(R.id.tvTips);
        }
    }
}

这也是我的数据库中的结构

pkwftd7m

pkwftd7m1#

看起来您读取的路径的一个或多个子节点不是完整的Member2对象,而只是一个字符串值。解决此问题最简单的方法是在尝试从快照获取对象之前打印快照的键:

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for(DataSnapshot dataSnapshot:snapshot.getChildren()){
            Log.i("Firebase",                               // 👈 Log the key and value
              "Reading Member2 from "+dataSnapshot.getKey() // 👈 to know where the
              +", value="+dataSnapshot.getValue()           // 👈 problem is in your
            );                                              // 👈 data
            Member2 user = dataSnapshot.getValue(Member2.class);
            list.add(user);
        }

        adapter.notifyDataSetChanged();

    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException();                          // 👈 never ignore errors
    }
});

相关问题