Java datasnapshot.getvalue()不工作并使应用程序崩溃
我正在尝试以Java datasnapshot.getvalue()不工作并使应用程序崩溃,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我正在尝试以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-arg
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)
以下是BuiInformation.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;
}
}
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;
}
}
谢谢你的回复。异常现在更改为:com.google.firebase.database.DatabaseException:Class com.google.android.gms.maps.model.LatLng未定义无参数构造函数。如果您使用的是ProGuard,请确保这些构造函数没有被剥离。这是另一个问题。如果答案回答了你的问题,你应该接受。关于以下问题,请查看答案。感谢您的回复。异常现在更改为:com.google.firebase.database.DatabaseException:Class com.google.android.gms.maps.model.LatLng未定义无参数构造函数。如果您使用的是ProGuard,请确保这些构造函数没有被剥离。这是另一个问题。如果答案回答了你的问题,你应该接受。关于以下问题,请查看答案。