Firebase无法使用类检索数据 [英] Firebase unable to retrieve data using class

查看:35
本文介绍了Firebase无法使用类检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些功能齐全的代码行,可检索每个单个数据,但无法使用类来检索它们.例如:这些行运行正常.

I have some fully functional lines of code retrieving each single data, but failed to retrieve them using class. Eg: These lines are working perfectly.

double value = (double) ds.child("player1score").getValue();
long value = (long) ds.child("point").getValue();

但是,当使用类进行检索时,会发生错误.这是我的代码的整个图片,不确定是否足以解决问题,请随时让我知道还需要什么,非常感谢您的任何建议,谢谢!

Yet, when retrieving using class, error occurs. Here are the whole picture of my codes, not sure if it is sufficient for solving the question, feel free to let me know what else is needed, appreciate so much for any advice, thanks!

Round().class

Round().class

public class Round {
public double player1score;
public double player2score;
public double player3score;
public double player4score;
public long point;

//Constructor
public Round(double player1score, double player2score, double player3score, double player4score, long point) {
    this.player1score = player1score;
    this.player2score = player2score;
    this.player3score = player3score;
    this.player4score = player4score;
    this.point = point;

//Below are All the getter and setter etc
}

我的MainActivity.class.onCreate()

My MainActivity.class.onCreate()

//Declare Variables
UserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference gameInfoRef = rootRef.child("user").child(UserId).child("gameInfo");
DatabaseReference gameRecordRef = rootRef.child("user").child(UserId).child("gameRecord");

String gameKey = "-LLyXhetqj9mj5fgh9bn";

在onCreate()下:

Under onCreate():

gameRecordRef.child(gameKey).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                ListView lv_history = (ListView) findViewById(R.id.lv_history);

                ArrayList<Round> ResultList = new ArrayList<>();

                //This line is where the error pointed to
                Round round = (Round) ds.getValue(Round.class);

                ResultList.add(round);

                ivd_HistoryAdapter adapter = new ivd_HistoryAdapter(id_History.this, ResultList);
                lv_history.setAdapter(adapter);
            }
        }

文本中的Firebase结构:

Firebase structure in text:

  "user": 
       "5xGKRXeHgThQy70lduPEp3mosTj1": 
             "gameRecord": 
                   "-LLyXhetqj9mj5fgh9bn": 
                        "player1score": 0.5,
                        "player2score": 0.5,
                        "player3score": 0.5,
                        "player4score": 0.5,
                        "point": 5

Logcat错误:

com.google.firebase.database.DatabaseException:类viwil.mahjongcal.Round没有定义无参数的构造函数.如果您使用的是ProGuard,请确保未剥离这些构造函数.com.google.android.gms.internal.zzelx.zze(未知来源)在com.google.android.gms.internal.zzelw.zzb(未知来源)com.google.android.gms.internal.zzelw.zza上(未知来源)位于com.google.firebase.database.DataSnapshot.getValue(未知来源)在viwil.mahjongcal.id_History $ 1.onDataChange(id_History.java:51)位于com.google.android.gms.internal.zzegf.zza(未知来源)com.google.android.gms.internal.zzeia.zzbyc(未知来源)com.google.android.gms.internal.zzeig.run(未知来源)在android.os.Handler.handleCallback(Handler.java:761)在android.os.Handler.dispatchMessage(Handler.java:98)在android.os.Looper.loop(Looper.java:156)在android.app.ActivityThread.main(ActivityThread.java:6523)在java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:942)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

com.google.firebase.database.DatabaseException: Class viwil.mahjongcal.Round does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped. at com.google.android.gms.internal.zzelx.zze(Unknown Source) at com.google.android.gms.internal.zzelw.zzb(Unknown Source) at com.google.android.gms.internal.zzelw.zza(Unknown Source) at com.google.firebase.database.DataSnapshot.getValue(Unknown Source) at viwil.mahjongcal.id_History$1.onDataChange(id_History.java:51) at com.google.android.gms.internal.zzegf.zza(Unknown Source) at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source) at com.google.android.gms.internal.zzeig.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:761) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

错误指向此行:(id_History.java:51)

Error pointed to this line: (id_History.java:51)

    Round round = ds.getValue(Round.class);

Firebase屏幕截图:

Firebase screenshot:

推荐答案

您遇到以下错误:

FATAL EXCEPTION: ... does not define a no-argument constructor

因为您的 Round 类未定义无参数构造函数.

JavaBeans 需要无参数的构造器出现在模型类中.

JavaBeans require a no-argument constructor to be present in the model class.

在Java中,当一个类根本没有构造函数时,Java编译器会自动添加一个默认的 no-argument构造函数.在类中定义 any 构造函数后,默认的无参数构造函数就会消失.

In Java, when a class has no constructors at all, there is a default no-argument constructor automatically added by the Java compiler. The moment you define any constructor in the class, the default no-argument constructor goes away.

在您的代码中,您的 Round 类定义了一个包含五个参数的构造函数:

In your code, your Round class defines such a constructor that contains five arguments:

public Round(double player1score, double player2score, double player3score, double player4score, long point) {}

只要此构造函数存在并且您没有定义无参数构造函数,该类就不会有一个.

As long as this constructor is present and you don't define a no-argument constructor, that class will not have one.

要解决此问题,您可以从类中删除该构造函数,或者手动添加无参数构造函数,如下所示:

To solve this, you either remove that constructor from the class, or manually add a no-argument constructor as shown below:

public Round() {}

当Firebase Realtime数据库SDK反序列化来自数据库的对象时,它要求所有使用中的对象都具有此构造函数,以便它可以实例化该对象.通过使用公共设置方法或直接访问公共成员来设置对象中的字段.

When the Firebase Realtime database SDK deserializes objects that are coming from the database, it requires that any objects in use, to have this constructor, so it can use it to instantiate the object. Fields in the objects are set by using public setter methods or direct access to public members.

您的 Round 类没有公共的 no-arg构造函数,因此SDK并不真正知道如何创建其实例.因此,必须拥有它.

Your Round class dosen't have a public no-arg constructor, the SDK doesn't really know how to create an instance of it. So it is mandatory to have it.

还请注意,不需要使用setter和getter.设置器始终是可选的,因为如果没有JSON属性的设置器,则Firebase客户端会将值直接设置到字段上.也不需要带参数的构造函数.两者都是惯用的,在没有它们的情况下有很好的案例.如果您将这些字段设为公开,则吸气剂也是可选的.

Also please note that setters and getter are not required. Setters are always optional because if there is no setter for a JSON property, the Firebase client will set the value directly onto the field. A constructor-with-arguments is also not required. Both are idiomatic and there are good cases to have classes without them. If you make the fields public, the getters are optional too.

这篇关于Firebase无法使用类检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆