我的对象包含String,long和int。对象叫做Tracker,我有一个跟踪器的arrayList。当我尝试使用toJson时,我得到一个错误。与fromJson相同。我每次都会得到这个。
FATAL EXCEPTION: main
Process: com.example.unknownuser.scrapstable, PID: 13550
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.unknownuser.scrapstable/com.example.unknownuser.scrapstable.MainActivity}: java.lang.IllegalArgumentException: class android.content.res.ColorStateList declares multiple JSON fields named mChangingConfigurations
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
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:170)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:853)
at com.google.gson.Gson.fromJson(Gson.java:802)
at com.example.unknownuser.scrapstable.MainActivity.onCreate(MainActivity.java:109)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
我有活动A试图保存跟踪器的ArrayList:
Tracker trax = new Tracker(description, timeToCount, imageID);
track.add(trax);
sharedPref = getSharedPreferences("pref", Context.MODE_PRIVATE);
editor = sharedPref.edit();
Gson gson = new Gson();
String json = gson.toJson(track);
editor.putString("LeftoverList", json);
editor.commit();
在活动B中,我尝试检索跟踪器的ArrayList
ArrayList<Tracker> foodList;
sharedPref = getSharedPreferences("pref", Context.MODE_PRIVATE);
gson = new Gson();
json = sharedPref.getString("LeftoverList", " ");
type = new TypeToken<ArrayList<Tracker>>(){}.getType();
foodList = gson.fromJson(json, type);
使用方法toJson
和fromJson
的行给了我致命的例外。非常感谢帮助。
答案 0 :(得分:1)
看起来您正在尝试将列表转换为json作为对象而不是列表。您可以创建一个包装类来保存列表并序列化/反序列化 。
创建一个简单的类,作为List的包装器。
class TrackWrapper {
List<Tracker> trackList;
public TrackWrapper(List<Tracker> tracks){
this.trackList = tracks;
}
}
将项目添加到列表
Tracker trax = new Tracker(description, timeToCount, imageID);
track.add(trax);
创建包装类的实例并在
中传递列表TrackWrapper wrapper = new TrackWrapper(trax);
序列化包装器
Gson gson = new Gson();
String json = gson.toJson(wrapper);
答案 1 :(得分:0)
您可以查看我的SharedPreferences helper
// Save list object to another SharedPreference
SharedPreferencesManager.getInstance(AnotherPreferenceName).putValue("guns", guns);
List<Gun> datas = SharedPreferencesManager.getInstance(AnotherPreferenceName).getValues("guns", Gun[].class); // get list object