我正在尝试使用Sharedpreferences保存对象的数组列表,但是toJson和fromJson导致致命异常 [英] I'm tryin to use Sharedpreferences to save an arraylist of objects, but toJson and fromJson are causing fatal exceptions
问题描述
我的对象包含一个String,long和int.对象称为跟踪器,我有一个跟踪器的arrayList.当我尝试使用toJson时出现错误. fromJson也是如此.我每次都得到这个.
My object contains a String, long, and int. Object is called Tracker and I have an arrayList of Trackers. When I try to use toJson i get an error. The same with fromJson. I get this everytime.
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,试图保存Trackers的ArrayList:
I have activity A that tries to save the ArrayList of Trackers:
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中,我尝试检索Trackers的ArrayList
In Activity B, I try to retrieve the ArrayList of Trackers
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
的行给了我致命的异常.非常感谢任何帮助的人.
The lines using the methods toJson
and fromJson
are giving me the fatal exceptions. Thanks so much for whoever helps.
推荐答案
您似乎正在尝试将列表作为对象而不是列表转换为json.您可以只创建一个包装器类来保存您的列表并序列化/反序列化那个.
It looks like you're trying to convert a list to json as an object instead of a list. You could just create a wrapper class to hold your list and serialize/deserialize that.
创建一个简单的类,用作列表的包装器.
Create a simple class that acts as a wrapper for your List.
class TrackWrapper {
List<Tracker> trackList;
public TrackWrapper(List<Tracker> tracks){
this.trackList = tracks;
}
}
将项目添加到列表
Tracker trax = new Tracker(description, timeToCount, imageID);
track.add(trax);
创建包装类的实例,然后将列表传递给
Create an instance of your wrapper class and pass your list in
TrackWrapper wrapper = new TrackWrapper(trax);
序列化包装器
Gson gson = new Gson();
String json = gson.toJson(wrapper);
这篇关于我正在尝试使用Sharedpreferences保存对象的数组列表,但是toJson和fromJson导致致命异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!