How can I serialize my User class for the Firebase

2019-01-07 01:30发布

问题:

I'm trying to retrieve the updated User from the dataSnapshot of the Firebase Database:

mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User atualizedUser = dataSnapshot.getValue(User.class);
            fName.setText(atualizedUser.getFirstName());
            lName.setText(atualizedUser.getLastName());
            profilePhoto.setProfileId(atualizedUser.getProfileId());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

But I get this error below:

com.google.firebase.database.DatabaseException: Class bqs.bomqueso.Model.User is missing a constructor with no arguments
                                                            at com.google.android.gms.internal.zzbtg$zza.zze(Unknown Source)
                                                            at com.google.android.gms.internal.zzbtg$zza.zzaH(Unknown Source)
                                                            at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
                                                            at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                                                            at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                            at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                            at bqs.bomqueso.Activities.MainActivity$3.onDataChange(MainActivity.java:92)
                                                            at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                                                            at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
                                                            at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                                                            at android.os.Handler.handleCallback(Handler.java:746)
                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                            at android.os.Looper.loop(Looper.java:148)
                                                            at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                            at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                            at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Here my User class (this is not a subclass, I created it in a separate file):

public class User {

private String firstName;
private String lastName;
private String profileId;

public User() { }

    public User(String fName, String lName, String id) {
        this.firstName = fName;
        this.lastName = lName;
        this.profileId = id;
    }

    public String getProfileId() {
        return profileId;
    }

    public void setProfileId(String id) { 
        this.profileId = id; 
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lName) { 
        this.lastName = lName; 
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String fName) { 
        this.firstName = fName; 
    }
}

I find similar questions here, but I hadn't found a solution for my problem, I hope you guys can help me, Thank you in advance!

回答1:

For Kotlin, if you're using data class like below for firebase,

data class UserModel(
        var UserName: String,
        var UserType: Int
)

then you'll get an error like

com.google.firebase.database.DatabaseException: UserModel does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.

So for this, you need to initialize its values.

data class UserModel(
        var UserName: String = "",
        var UserType: Int = 0
)


回答2:

Create a constructor with no arguments as

public User() { } as cricket mentioned and to serialize

public class User implements Serializable {

private String firstName;
private String lastName;
private String profileId;

public User() {
}

public User(String fName, String lName, String id) {
    this.firstName = fName;
    this.lastName = lName;
    this.profileId = id;

}
public String getProfileId() {
    return profileId;
}

public void setProfileId(String id) {
    this.profileId = id;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lName) {
    this.lastName = lName;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String fName) {
    this.firstName = fName;
}

}

If this didn't meet requirement note down