gpt4 book ai didi

java - datasnapshot.getvalue() 不起作用并导致应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 01:55:43 24 4
gpt4 key购买 nike

我正在尝试以 BusInformation.class 的形式从 firebase 实时数据库获取数据。但是我的应用程序在设置快照的类类型时崩溃了:ds.getValue(BusInformation.class);

我收到以下错误:

com.google.firebase.database.DatabaseException: Class com.example.muzammil.bustracking.app_classes.BusInformation 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.firebase_database.zzku.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)

以下是 BuInformation.java 类的代码:

public class BusInformation {
private String id, username,phone;
private LatLng position;
private String destination;

public BusInformation(String id, String username, String phone, String destination,LatLng position) {
this.id = id;
this.username = username;
this.phone = phone;
this.position = position;
this.destination = destination;
}
}

在 Firebase 数据库中,数据完全以这种形式存在。

从firebase获取数据的代码:

 public void getUpdatesFromFirebase(DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
if(ds.exists()) {
busInfo = ds.getValue(BusInformation.class); //App crashes at this point

}

}
}

任何人请告诉我代码有什么问题......

最佳答案

BusInformation 未定义无参构造函数。

手动向其添加无参数构造函数:public BusInformation(){}

你的类应该是这样的:

public class BusInformation {
private String id, username,phone;
private LatLng position;
private String destination;

//no-argument constructor
public BusInformation(){}

public BusInformation(String id, String username, String phone, String destination,LatLng position) {
this.id = id;
this.username = username;
this.phone = phone;
this.position = position;
this.destination = destination;
}
}

关于java - datasnapshot.getvalue() 不起作用并导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52344088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com