【发布时间】:2018-09-10 15:59:49
【问题描述】:
我有一些功能齐全的代码行检索每个数据,但未能使用类检索它们。例如:这些行运行良好。
double value = (double) ds.child("player1score").getValue();
long value = (long) ds.child("point").getValue();
然而,当使用类检索时,会发生错误。 这是我的代码的全貌,不确定是否足以解决问题,请随时让我知道还需要什么,非常感谢任何建议,谢谢!
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()
//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);
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 结构:
"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)
错误指向这一行:(id_History.java:51)
Round round = ds.getValue(Round.class);
Firebase 截图:
【问题讨论】:
标签: java android firebase firebase-realtime-database