datasnapshot.getvalue()不起作用,使应用程序崩溃

时间:2018-09-15 11:05:43

标签: 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-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

            }

        }
    }

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

1 个答案:

答案 0 :(得分:2)

BusInformation does not define a no-argument constructor.

手动向其添加一个无参数的构造函数: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;
    }
}