本文介绍了ANDROID FirebaseDatabase:无法保存对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将对象添加到Firebase实时数据库。我试了下面的代码。但是,当这个代码执行时,你可能会遇到这样的情况:

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

我的应用崩溃了

  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;




$ b $ p $这些是错误。
$ b

这是我如何保存对象。

  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>任务){
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.dismissW ithAnimation();
suc = new SweetAlertDialog(AddMember.this,SweetAlertDialog.ERROR_TYPE);
suc.setTitleText(Failed);
suc.setContentText(保存数据失败);
suc.setConfirmText(再试一次);
suc.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener(){
@Override
public void onClick(SweetAlertDialog sweetAlertDialog){
savePerson();

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





解决方案

堆栈跟踪指的是 isChangingConfigurations ,它是 Activity 方法的名称。这可能表示您已经将类 Person 声明为活动的内部类。当Firebase序列化 Person 时,它也会尝试序列化封闭的Activity类并失败。
$ b 移动 Person 来打包级别或更改声明以使其成为静态内部类: public static class Person


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.

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


解决方案

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.

这篇关于ANDROID FirebaseDatabase:无法保存对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 15:57