我有一个单个的类型A的自定义对象和一个包含要存储在gson的“共享首选项”中的类型B的自定义对象的ArrayList。对象A正常工作,但是当我尝试存储对象B的列表甚至仅存储对象B的一个实例时,会出现以下错误消息:
2019-01-07 13:05:21.610 28295-28295/com.example.aev.quizzle E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aev.quizzle, PID: 28295
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aev.quizzle/com.example.aev.quizzle.activities.MapsActivity}: java.lang.IllegalArgumentException: class android.content.res.ColorStateList declares multiple JSON fields named mChangingConfigurations
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.IllegalArgumentException: class android.content.res.ColorStateList declares multiple JSON fields named mChangingConfigurations
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at com.google.gson.Gson.getAdapter(Gson.java:458)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at com.google.gson.Gson.getAdapter(Gson.java:458)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
at com.google.gson.Gson.getAdapter(Gson.java:458)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:56)
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.Gson.toJson(Gson.java:704)
at com.google.gson.Gson.toJson(Gson.java:683)
at com.google.gson.Gson.toJson(Gson.java:638)
at com.google.gson.Gson.toJson(Gson.java:618)
at com.example.aev.quizzle.activities.MapsActivity.generateThemes(MapsActivity.java:592)
at com.example.aev.quizzle.activities.MapsActivity.onCreate(MapsActivity.java:164)
at android.app.Activity.performCreate(Activity.java:7383)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
我这样存储数组列表:
themes = new ArrayList<>();
themes.add(new Theme(0, this));
themes.add(new Theme(1, this));
themes.add(new Theme(2, this));
String jsonTheme = gson.toJson(themes);
prefsEditor.putString("themes", jsonTheme);
prefsEditor.commit();
我的班级主题看起来像这样:
public class Theme implements Parcelable {
public ImageView icon;
public int themeID;
//public Question[] questions = new Question[14];
public List<Question> questions = new ArrayList<>();
boolean unlocked;
Context context;
我在超级类和应该存储的对象中看到了类似的问题,变量名称相同,但是就我而言,我没有在Theme类中扩展另一个类。 错误指向
行String jsonTheme = gson.toJson(themes);
我对gson对象的处理错误还是错过了什么?
答案 0 :(得分:1)
使用@Transient
是解决此序列化问题的一种方法。正如您在反序列化之后可能已经注意到的那样,您需要将ImageView
和Context
设置为每个反序列化的Theme
,这可能是一个小问题。
您还可以考虑将要保留的数据与诸如视图和上下文之类的暂时性内容分开。在只看到一小部分代码后,您的结构可能类似于:
类似的东西:
public class Theme {
public ImageView icon;
Context context;
ThemeData themeData
public Theme(Context contect, ImageView icon, ThemeData themeData) {
this.context = context;
this.icon = icon;
themeData = themeData;
}
public void doSomethingWithThemeDataAndViewAndContext() {
// whatever you need to do in Theme
}
}
其中ThemeData
包含所有非瞬态字段,然后您将保留它们到SharedPreferences
:
public class ThemeData {
public int themeID;
//public Question[] questions = new Question[14];
public List<Question> questions = new ArrayList<>();
boolean unlocked;
}
还请注意:我认为,如果您使用GSON序列化主题并将其另存为SharedPreferences
中的JSON字符串,则无需实现Parcelable
,因此我将其删除。