我正在尝试将对象上传到Firebase数据库,但是当我这样做时,我得到了一个stackoverflower错误。我看到很多人得到这个错误,但他们的解决方案在我的情况下不起作用。我唯一能做的就是编写一个String变量。
我尝试:检查所有我的对象变量是Firebase序列化允许的
这是代码:
对象
package chtecnologies.lola.entities;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.io.Serializable;
public class Pet {
private String id;
private String name;
private String breed;
private String type;
private String pictures;
private String description;
private String location;
private String locality;
private String gender;
private String age;
private String userid;
public Pet(String id, String name, String breed, String type, String description,
String pictures, String location, String locality, String gender,
String age, String userid) {
this.id = id;
this.name = name;
this.breed = breed;
this.type = type;
this.description = description;
this.pictures = pictures;
this.location = location;
this.locality = locality;
this.gender = gender;
this.age = age;
this.userid = userid;
}
public Pet(String breed, String type, String pictures, String description, String location, String locality, String gender, String age, String userid) {
/** CONSTRUCTOR FOR ADOPTABLE PET**/
this.breed = breed;
this.type = type;
this.pictures = pictures;
this.description = description;
this.location = location;
this.locality = locality;
this.gender = gender;
this.age = age;
this.userid = userid;
}
public Pet(String name, String breed, String type, String pictures, String description, String location, String locality, String userid) {
/** CONSTRUCTOR FOR LOST PET**/
this.name = name;
this.breed = breed;
this.type = type;
this.pictures = pictures;
this.description = description;
this.location = location;
this.locality = locality;
this.userid = userid;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPictures() {
return pictures;
}
public void setPictures(String pictures) {
this.pictures = pictures;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getLocality(){ return locality; }
public void setLocality(String locality){
this.locality = locality;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Pet getPet(){
return this;
}
@Override
public String toString() {
return "Pet{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", breed='" + breed + '\'' +
", type='" + type + '\'' +
", pictures=" + pictures +
", description='" + description + '\'' +
", location='" + location + '\'' +
", locality='" + locality + '\'' +
", gender='" + gender + '\'' +
", age='" + age + '\'' +
", userid='" + userid + '\'' +
'}';
}
}
写入数据库的方法:
@Override
public void postPet(Pet pet) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference();
// TODO: Next line throwing stackoverflow error
ref.push().setValue(pet);
Intent intent = new Intent(mContext, MainActivity.class);
mContext.startActivity(intent);
}
Logcat
FATAL EXCEPTION: main
Process: chtecnologies.lola, PID: 11761
java.lang.StackOverflowError: stack size 8MB
at java.lang.reflect.Method.invoke(Native Method)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:58)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
03-20 12:28:55.008 11761-11761/chtecnologies.lola E/AndroidRuntime: at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzca(Unknown Source:0)
at com.google.android.gms.internal.zzelx.zzcb(Unknown Source:120)
at com.google.android.gms.internal.zzelw.zzbx(Unknown Source:237)
at com.google.android.gms.internal.zzelw.zzbw(Unknown Source:0)
at com.google.firebase.database.DatabaseReference.zza(Unknown Source:10)
at com.google.firebase.database.DatabaseReference.setValue(Unknown Source:7)
at chtecnologies.lola.ui.activities.LoadPresenter.postPet(LoadPresenter.java:144)
at chtecnologies.lola.ui.activities.LoadPresenter.checkFieldsLost(LoadPresenter.java:132)
at chtecnologies.lola.ui.activities.PostLostActivity$3.onClick(PostLostActivity.java:101)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
我删除了一些相同的行,因为我超出了字符
我是Firebase的新手,我试图将我的实际存储数据库更改为Firebase,但我无法使其工作。我阅读了文档,我看不到我做的任何事情都没有被允许。我希望你能帮助我解决这个问题。谢谢!
答案 0 :(得分:2)
您需要在Pet
课程中添加无参数构造函数,因为Firebase需要这样做。
public Pet() {}
并删除这些行:
public Pet getPet(){
return this;
}
当Firebase实时数据库SDK对来自数据库的对象进行反序列化时,它要求所有正在使用的对象都具有public no-argument constructor
,以便它可以使用它来实例化对象。通过使用公共setter方法或直接访问公共成员来设置对象中的字段。
如果你的宠物对象没有public no-arg constructor
,那么SDK并不真正知道如何创建它的实例。因此必须拥有它。