Java Firebase 在类上找不到要序列化的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37743661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firebase No properties to serialize found on class
提问by EulerVen
I'm bloqued creating a Firebase Database.
我正在创建一个 Firebase 数据库。
I'm trying to model a class. A very simple class:
我正在尝试为一个班级建模。一个非常简单的类:
package com.glups.model;
import com.google.firebase.database.IgnoreExtraProperties;
@IgnoreExtraProperties
public class AlumnoFB {
private String nombre;
private String apellidos;
private String telefono;
private String email;
private Boolean tieneWhatsapp;
private Boolean tieneTelegram;
private Boolean tieneHangouts;
private Long formaPago;
private Double ratioHora;
private Double precioHora;
private Double horasCompensadas;
public AlumnoFB() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public AlumnoFB(String nombre,
String apellidos,
String telefono,
String email,
Boolean tieneWhatsapp,
Boolean tieneTelegram,
Boolean tieneHangouts,
Long formaPago,
Double ratioHora,
Double precioHora,
Double horasCompensadas) {
this.nombre = nombre;
this.apellidos = apellidos;
this.telefono = telefono;
this.email = email;
this.tieneWhatsapp = tieneWhatsapp;
this.tieneTelegram = tieneTelegram;
this.tieneHangouts = tieneHangouts;
this.formaPago = formaPago;
this.ratioHora = ratioHora;
this.precioHora = precioHora;
this.horasCompensadas = horasCompensadas;
}
}
that is almost like a sample class from Firebase.
这几乎就像来自 Firebase 的示例类。
Application user obtained from getUser() has been logged on Firebase.
从 getUser() 获取的应用程序用户已登录 Firebase。
When I call SetValue:
当我调用 SetValue 时:
AlumnoFB alumno = new AlumnoFB("", "", "", "", false, false, false, ((Integer)FormaPago.INT_NO_PAGADO).longValue(), 0.0, 0.0, 0.0);
mDatabase.child("AlumnoFB").child(ControlClasesFirebase.getUser().getUid()).setValue(alumno) ;
A fatal exception raises.
引发致命异常。
06-10 10:17:37.179 13841-13841/com.glups.controlclases E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.glups.controlclases, PID: 13841 com.google.firebase.database.DatabaseException: No properties to serialize found on class com.glups.model.AlumnoFB
at com.google.android.gms.internal.zzaix$zza.<init>(Unknown Source)
at com.google.android.gms.internal.zzaix.zzj(Unknown Source)
at com.google.android.gms.internal.zzaix.zzaw(Unknown Source)
at com.google.android.gms.internal.zzaix.zzav(Unknown Source)
at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
at com.glups.controlclases.MainActivity.onClick(MainActivity.java:305)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5258)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I've checked types, and all are accepted. What's wrong?
我检查了类型,所有类型都被接受。怎么了?
采纳答案by Shubhank
Firebase require your Pojo to have public variables or getter/setter.
Firebase 要求您的 Pojo 具有公共变量或 getter/setter。
Change variable declarations to public
将变量声明更改为 public
public String nombre;
public String apellidos;
public String telefono;
public String email;
public Boolean tieneWhatsapp;
public Boolean tieneTelegram;
public Boolean tieneHangouts;
public Long formaPago;
public Double ratioHora;
public Double precioHora;
public Double horasCompensadas;
回答by aselims
If you are using proguard, some methods in the model could be stripped out depending on your configuration. As we know that there is no much optimization to POJO as it has only fields with getters and/or (optionally) setters, you can use the annotation "@Keep" so proguard will not delete any methods from this class.
如果您使用 proguard,模型中的某些方法可能会根据您的配置被删除。我们知道 POJO 没有太多优化,因为它只有带有 getter 和/或(可选)setter 的字段,您可以使用注释“@Keep”,因此 proguard 不会从此类中删除任何方法。
Check this for more info: https://developer.android.com/studio/build/shrink-code.html
查看更多信息:https: //developer.android.com/studio/build/shrink-code.html
@Keep
public class Store {}
回答by Mahmudul Hasan Shohag
In my case I forgot to add a proguard rule to keep the model classes:
就我而言,我忘记添加 proguard 规则来保留模型类:
-keep class com.google.firebase.example.fireeats.model.** { *; }
This is same as @aselims's answer, just proguard version.
这与@aselims 的答案相同,只是 proguard 版本。
I found it in the official firestore example:
我在官方firestore示例中找到了它:
https://github.com/firebase/quickstart-android/blob/master/firestore/app/proguard-rules.pro
https://github.com/firebase/quickstart-android/blob/master/firestore/app/proguard-rules.pro
回答by Vishal16
I have got this issue today and just resolve this providing getter/setter for private variables.
我今天遇到了这个问题,只是解决了这个为私有变量提供 getter/setter 的问题。
ex:
前任:
private String pid;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
Now it's work perfectly without any error. Hope it will help newbies devs.
现在它完美地工作,没有任何错误。希望对新手开发者有所帮助。
回答by Marium Jawed
Declare variables as public.
将变量声明为公共变量。
public String email
回答by PGMacDesign
In case anyone runs into this and none of the above solutions work, the variables declared also need to be of Object
type and not raw types.
如果有人遇到这种情况并且上述解决方案都不起作用,则声明的变量也需要是Object
类型而不是原始类型。
For example, you canuse Integer
's but you cannotuse int
's.
例如,您可以使用Integer
's,但不能使用int
's。
Same is true for boolean
, float
, double
, etc. Basically, any unboxed type.
同样是真实的boolean
,float
,double
等,基本上任何拆箱类型。
回答by QDiamond
adding
添加
@ServerTimeStamp
public Date date;
helped me to resolve the issue
帮我解决了这个问题