Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java datasnapshot.getvalue()不工作并使应用程序崩溃_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

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,请确保这些构造函数没有被剥离。这是另一个问题。如果答案回答了你的问题,你应该接受。关于以下问题,请查看答案。