我已经多次使用Gson了,这是我第一次遇到这样的问题,一旦我想将对象写入共享首选项,应用程序在logcat中没有任何异常崩溃,请你帮助我
我正在保存像这样的对象(就像我一直这样):
mShared = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
edit = mShared.edit();
Gson gson = new Gson();
String json = gson.toJson(currentTeam);
edit.putString(getResources().getString(R.string.current_team), json).apply();
这是课程:
GameType.java
public class GameType implements Parcelable{
private int id;
private String lookupCode, name, namePS, icon;
private List<Team> teams = new ArrayList<Team>();
public GameType(){
}
public List<Team> getTeams() {
return teams;
}
public void setTeams(List<Team> teams) {
this.teams = teams;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLookupCode() {
return lookupCode;
}
public void setLookupCode(String lookupCode) {
this.lookupCode = lookupCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNamePS() {
return namePS;
}
public void setNamePS(String namePS) {
this.namePS = namePS;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(lookupCode);
dest.writeString(name);
dest.writeString(namePS);
dest.writeString(icon);
dest.writeList(teams);
}
// "De-parcel object
private GameType(Parcel in) {
id = in.readInt();
lookupCode=in.readString();
name=in.readString();
namePS = in.readString();
icon = in.readString();
in.readList(teams,getClass().getClassLoader());
}
public static final Parcelable.Creator CREATOR
= new Parcelable.Creator() {
public GameType createFromParcel(Parcel in) {
return new GameType(in);
}
public GameType[] newArray(int size) {
return new GameType[size];
}
};
}
Team.java
public class Team implements Parcelable{
private int id;
private String namePs, logo , nameEn, textColor, firstColor, secondColor, thirdColor;
private GameType game;
public Team(){
id = -1;
namePs = "";
logo = "";
nameEn = "";
textColor = "";
firstColor = "";
secondColor = "";
thirdColor = "";
game = new GameType();
}
public Team(int id, String logo, String namePs, String nameEn, String textColor, String firstColor, String secondColor, String thirdColor) {
this.id = id;
this.namePs = namePs;
this.nameEn = nameEn;
this.logo = logo;
this.textColor = textColor;
this.firstColor = firstColor;
this.secondColor = secondColor;
this.thirdColor = thirdColor;
}
public void setGame(GameType game) {
this.game = game;
}
public GameType getGame() {
return game;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNamePs() {
return namePs;
}
public void setNamePs(String nameFa) {
this.namePs = nameFa;
}
public String getNameEn() {
return nameEn;
}
public void setNameEn(String nameEn) {
this.nameEn = nameEn;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public String getTextColor() {
return textColor;
}
public void setTextColor(String textColor) {
this.textColor = textColor;
}
public String getFirstColor() {
return firstColor;
}
public void setFirstColor(String firstColor) {
this.firstColor = firstColor;
}
public String getSecondColor() {
return secondColor;
}
public void setSecondColor(String secondColor) {
this.secondColor = secondColor;
}
public String getThirdColor() {
return thirdColor;
}
public void setThirdColor(String thirdColor) {
this.thirdColor = thirdColor;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(namePs);
dest.writeString(logo);
dest.writeString(nameEn);
dest.writeString(textColor);
dest.writeString(firstColor);
dest.writeString(secondColor);
dest.writeString(thirdColor);
dest.writeParcelable(game, flags);
}
// "De-parcel object
private Team(Parcel in) {
id = in.readInt();
namePs = in.readString();
logo = in.readString();
nameEn = in.readString();
textColor = in.readString();
firstColor = in.readString();
secondColor = in.readString();
thirdColor = in.readString();
game = in.readParcelable(getClass().getClassLoader());
}
public static final Parcelable.Creator CREATOR
= new Parcelable.Creator() {
public Team createFromParcel(Parcel in) {
return new Team(in);
}
public Team[] newArray(int size) {
return new Team[size];
}
};
}
这是Log cat:
FATAL EXCEPTION: main
Process: com.ids.a3090, PID: 20391
java.lang.StackOverflowError: stack size 8MB
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:148)
at java.lang.StringBuffer.append(StringBuffer.java:219)
at java.io.StringWriter.write(StringWriter.java:147)
at com.google.gson.stream.JsonWriter.string(JsonWriter.java:566)
at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402)
at com.google.gson.stream.JsonWriter.value(JsonWriter.java:417)
at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:422)
at com.google.gson.internal.bind.TypeAdapters$16.write(TypeAdapters.java:406)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:976)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(Refle
答案 0 :(得分:1)
您有循环参考。 Team
上有GameType
的{{1}}引用{/ 1}}。