本文介绍了如何为Firebase数据库异常添加内置类的无参数构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Android中的Firebase Realtime
数据库中获取FirebaseLocationData对象。

I am trying to fetch the FirebaseLocationData object from Firebase Realtime Database in Android.

根据Docs,该类应该没有参数构造函数,已完成
public FirebaseLocationData(){} ,但仍显示错误

According to Docs, the class should have no arguments constructor which i have done aspublic FirebaseLocationData() {} but it is still showing error

com.google.firebase.database.DatabaseException:android.location.Location类未定义无参数构造函数。如果您使用的是ProGuard,请确保没有剥离这些构造函数。

我应该如何制作一个无参数的构造函数 android.location.Location 类?

How am i supposed to make a no-argument constructor of android.location.Location class?

此行产生错误 FirebaseLocationData fld = dataSnapshot .getValue(FirebaseLocationData.class);

代码:

 public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
            Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());

            FirebaseLocationData fld = dataSnapshot.getValue(FirebaseLocationData.class);
            Toast.makeText(MainActivity.this, fld.getEmail()+" added", Toast.LENGTH_SHORT).show();

        }

FirebaseLocationData类:

public class FirebaseLocationData {
String email;
Location location;
String time;

public FirebaseLocationData() {
}

public FirebaseLocationData(Location location, String email, String time) {
    this.location = location;
    this.email = email;
    this.time = time;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public Location getLocation() {
    return location;
}

public void setLocation(Location location) {
    this.location = location;
}

}

推荐答案

您正在尝试编写一个 android.location.Location 对象和 类没有的对象一个无参数的公共构造函数。许多Android库类无法满足Firebase的要求,因此您必须编写自己的序列化/反序列化才能与数据库交换必要的信息。

You're trying to write a android.location.Location object and that class doesn't have a public, no-argument constructor. Many Android library classes will not meet the requirements from Firebase, and you'll have to write your own serialization/deserialization to exchange the necessary information with the database.

另请参见:

这篇关于如何为Firebase数据库异常添加内置类的无参数构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:52