无法将gson转换为我的Object类型

时间:2015-08-28 00:44:20

标签: java android gson

我已经查看了同样错误的其他问题,但它们似乎都与该用户的情况有关。我无法找到解决问题的方法。

我在我的Android应用中使用gson将自定义对象列表保存到sharedPreferences中。对象非常基础:

public class Idea {
    private String title;
    private String details;

    public Idea (String title, String details) {
        this.title = title;
        this.details = details;
    }

    public String getTitle() { return title; }

    public void setTitle(String newTitle) { title = newTitle; }

    public String getDetails() { return details; }

    public void setDetails(String newDetails) { details = newDetails; }
}

列表声明如下:

private static List<Idea> ideas = new ArrayList<Idea>();

序列化似乎工作正常但是当我尝试在MainActivity中解码它时如此:

String jsonIdeas = sharedPref.getString(IDEAS_KEY, null);
Gson gson = new Gson();
ideas = gson.fromJson(jsonIdeas, ArrayList.class);

我收到此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zarwanhashem.ideatrackr/com.zarwanhashem.ideatrackr.MainActivity}: java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.zarwanhashem.ideatrackr.Idea
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.zarwanhashem.ideatrackr.Idea
            at com.zarwanhashem.ideatrackr.MainActivity.onCreate(MainActivity.java:79)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我不认为我在堆栈跟踪中获得的位置是准确的,因为每当我调试应用程序时它都会改变。但是通过IntelliJ中的断点指向和评估,我能够将错误缩小到解码上面发布的代码。

以防序列化实际上不行,这是我正在使用的代码:

Gson gson = new Gson();
String json = gson.toJson(ideas);
editor.putString(IDEAS_KEY, json); //putting it into sharedPreferences

当我使用它来存储字符串列表列表时,gson工作正常,但是当我将代码用于我的自定义对象时,我开始收到此错误。

0 个答案:

没有答案