Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANDROID FirebaseDatabase: Unable to save object

I Couldn't add an object to firebase realtime database. I tried the following code. But when this code executes

db.child("Users").setValue(person); 

my app crashes

public class Person{
   String fname;
   String lname;
   Person()
   {}
   Person(String fname,String lname)
   {
       this.fname = fname;
       this.lname = lname;
   }

   public String getFname() {
       return fname;
   }

   public String getLname() {
       return lname;
   }
}

Here is the ERROR.

11-20 15:34:57.762 2752-2752/shazdroid.com.surveyapp E/AndroidRuntime: FATAL EXCEPTION: main Process: shazdroid.com.surveyapp, PID: 2752 com.google.firebase.database.DatabaseException: Found conflicting getters for name: isChangingConfigurations at com.google.android.gms.internal.zzamy$zza.(Unknown Source) at com.google.android.gms.internal.zzamy.zzj(Unknown Source) at com.google.android.gms.internal.zzamy.zzca(Unknown Source) at com.google.android.gms.internal.zzamy.zzch(Unknown Source) at com.google.android.gms.internal.zzamy$zza.zzci(Unknown Source) at com.google.android.gms.internal.zzamy.zzca(Unknown Source) at com.google.android.gms.internal.zzamy.zzbz(Unknown Source) at com.google.firebase.database.DatabaseReference.zza(Unknown Source) at com.google.firebase.database.DatabaseReference.setValue(Unknown Source) at shazdroid.com.surveyapp.AddMember.savePerson(AddMember.java:207) at shazdroid.com.surveyapp.AddMember.access$000(AddMember.java:37) at shazdroid.com.surveyapp.AddMember$6.onClick(AddMember.java:193) at android.view.View.performClick(View.java:5610) at android.view.View$PerformClick.run(View.java:22260) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Here is how i'm saving the object.

    private void savePerson() {
    suc.show();
    Person person = new Person("shahbaz","ansari");
    db.child("User").setValue(person)
            .addOnCompleteListener(AddMember.this, new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if(task.isSuccessful())
            {
                suc.dismissWithAnimation();
                suc = new SweetAlertDialog(AddMember.this,SweetAlertDialog.SUCCESS_TYPE);
                suc.setTitleText("Successful");
                suc.setContentText("Saving data successful");
                suc.setConfirmText("OK");
                suc.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismissWithAnimation();
                    }
                }).show();
            }else{
                suc.dismissWithAnimation();
                suc = new SweetAlertDialog(AddMember.this,SweetAlertDialog.ERROR_TYPE);
                suc.setTitleText("Failed");
                suc.setContentText("Failed to save data");
                suc.setConfirmText("Try Again");
                suc.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        savePerson();

                    }
                });
                suc.setCancelText("cancle");
                suc.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismissWithAnimation();
                    }
                }).show();
            }
        }
    });

like image 316
Shahbaz Ansari Avatar asked Nov 20 '16 10:11

Shahbaz Ansari


1 Answers

The stack trace refers to isChangingConfigurations, which is the name of a method of Activity. This probably indicates that you have declared class Person as an inner class of an activity. When Firebase serializes Person it also tries to serialize the enclosing Activity class and fails.

Move the declaration of Person to package level or change the declaration to make it a static inner class: public static class Person.

like image 108
Bob Snyder Avatar answered Jan 04 '23 11:01

Bob Snyder