gpt4 book ai didi

android - 我正在尝试使用 Sharedpreferences 来保存对象的数组列表,但是 toJson 和 fromJson 导致致命异常

转载 作者:行者123 更新时间:2023-11-30 00:17:33 25 4
gpt4 key购买 nike

我的对象包含一个 String、long 和 int。对象称为 Tracker,我有一个 Tracker 数组列表。当我尝试使用 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)

我有 Activity A 试图保存 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();

在 Activity B 中,我尝试检索 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);

使用方法 toJsonfromJson 的行给了我致命的异常。非常感谢提供帮助的人。

最佳答案

看起来您正在尝试将列表转换为 json 作为对象而不是列表。您可以创建一个包装类来保存您的列表并序列化/反序列化那个

创建一个简单的类作为列表的包装器。

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);

关于android - 我正在尝试使用 Sharedpreferences 来保存对象的数组列表,但是 toJson 和 fromJson 导致致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46984561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com