Firebase 无法检索 class 的值
Firebase unable to retrieve value to class
我有一行代码可以检索每个数据,但未能将它们全部应用到 class。这条线路运行良好。
double value = (double) ds.child("player1score").getValue();
但是说到这里,就失败了,崩溃了。
int value = (int) ds.child("point").getValue();
通过将其更改为解决了此问题:
long value = (long ) ds.child("point").getValue();
然而,当使用 class 检索时,出现错误。
这是我的代码的全貌,非常感谢任何建议,谢谢!!
回合().class
public class Round {
public double player1score;
public double player2score;
public double player3score;
public double player4score;
public int point;
//Constructor
public Round(double player1score, double player2score, double player3score, double player4score, int 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()
//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()下:
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);
//It shows proper results in logcat
System.out.println(ds.getValue());
ArrayList<Round> ResultList = new ArrayList<>();
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 结构:
"user":
"5xGKRXeHgThQy70lduPEp3mosTj1":
"gameRecord":
"-LLyXhetqj9mj5fgh9bn":
"player1score": 0.5,
"player2score": 0.5,
"player3score": 0.5,
"player4score": 0.5,
"point": 5
Logcat 错误:(已编辑,不是由整数类型错误引起的)
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.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)
Round round = ds.getValue(Round.class);
Firebase 屏幕截图:
要解决此问题,请更改以下代码行:
int value = (int) ds.child("point").getValue();
到
long score = ds.child("point").getValue(Long.class);
您需要将该对象转换为 Long
类型的对象,而不是原始类型 int
。即使您在模型 class 中将 score
属性 定义为 int
,在数据库中 属性 存储为 long
.默认情况下,数字在 Firebase 实时数据库中存储为 long numbers 而 not 作为 ints
.
我有一行代码可以检索每个数据,但未能将它们全部应用到 class。这条线路运行良好。
double value = (double) ds.child("player1score").getValue();
但是说到这里,就失败了,崩溃了。
int value = (int) ds.child("point").getValue();
通过将其更改为解决了此问题:
long value = (long ) ds.child("point").getValue();
然而,当使用 class 检索时,出现错误。 这是我的代码的全貌,非常感谢任何建议,谢谢!!
回合().class
public class Round {
public double player1score;
public double player2score;
public double player3score;
public double player4score;
public int point;
//Constructor
public Round(double player1score, double player2score, double player3score, double player4score, int 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()
//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()下:
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);
//It shows proper results in logcat
System.out.println(ds.getValue());
ArrayList<Round> ResultList = new ArrayList<>();
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 结构:
"user":
"5xGKRXeHgThQy70lduPEp3mosTj1":
"gameRecord":
"-LLyXhetqj9mj5fgh9bn":
"player1score": 0.5,
"player2score": 0.5,
"player3score": 0.5,
"player4score": 0.5,
"point": 5
Logcat 错误:(已编辑,不是由整数类型错误引起的)
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.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)
Round round = ds.getValue(Round.class);
Firebase 屏幕截图:
要解决此问题,请更改以下代码行:
int value = (int) ds.child("point").getValue();
到
long score = ds.child("point").getValue(Long.class);
您需要将该对象转换为 Long
类型的对象,而不是原始类型 int
。即使您在模型 class 中将 score
属性 定义为 int
,在数据库中 属性 存储为 long
.默认情况下,数字在 Firebase 实时数据库中存储为 long numbers 而 not 作为 ints
.