Firebase-在类上找不到要序列化的属性 [英] Firebase - No properties to serialize found on class
问题描述
我有一个这样的班级:
class dataModel {
String id, name;
Integer count;
dataModel() {}
}
然后我从Firebase添加数据.
And I add data from Firebase.
mDatabase.addValueEventListener(mListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSet.add(dataSnapshot.getValue(dataModel.class));
//...
}
});
当我以调试方式运行应用程序时,没有问题.但是发布后,应用程序崩溃并显示错误:
When I run the app as debug, there is no problem. But after it is released, app crashes with the error:
com.google.firebase.database.DatabaseException: No properties to serialize found on class com.my.package.dataModel
我有 minifyEnabled true
I have minifyEnabled true
推荐答案
To solve this, your class needs to implement the Serializable interface:
class dataModel implements Serializable {}
您还需要添加带有三个参数的构造函数.您的课程应如下所示:
You also need to add a contructor with three arguments. Your class should look like this:
class dataModel implements Serializable {
private String id, name;
private Integer count;
@ServerTimestamp
private Date time;
dataModel() {}
dataModel(String id, String name, Integer count) {
this.id = id;
this.name = name;
this.count = count;
}
}
如果日期为null
,它将具有服务器生成的时间戳.因此,您无需为其设置值.另请参阅用于标记Date
字段的annotation
,该字段将使用服务器时间戳填充.
If the date is null
, it will have the server-generated timestamp. So you don't need to do set the value for it. Please also see the annotation
used to mark a Date
field to be populated with a server timestamp.
同样非常重要,不要忘记添加public getters
.
Also very important, don't forget to add public getters
.
,另一个要求是在proguard-rules.pro中添加-keepclassmembers class com.yourcompany.models.** { *; }
.
and the other requirement will be to add -keepclassmembers class com.yourcompany.models.** { *; }
in proguard-rules.pro.
如此处所述.
这篇关于Firebase-在类上找不到要序列化的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!