Firebase 在类上找不到要序列化的属性

新手上路,请多包涵

我对创建 Firebase 数据库感到困惑。

我正在尝试模拟一个班级。一个非常简单的类:

 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;
    }
}

这几乎就像来自 Firebase 的示例类。

从 getUser() 获得的应用程序用户已经登录到 Firebase。

当我调用 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) ;

引发致命异常。

 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$4.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)

我已经检查了类型,并且都被接受了。怎么了?

原文由 EulerVen 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 390
2 个回答

Firebase 要求您的 Pojo 具有 公共变量或 getter/setter。

将变量声明更改为 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;

原文由 Shubhank 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您使用混淆器,模型中的某些方法可能会根据您的配置被删除。正如我们所知,POJO 没有太多优化,因为它只有带有 getter 和/或(可选)setter 的字段,您可以使用注释“@Keep”,这样混淆器就不会删除此类中的任何方法。

检查此以获取更多信息: https ://developer.android.com/studio/build/shrink-code.html

 @Keep
public class Store {}

原文由 aselims 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题