将JsonString转换为自定义java ArrayList

时间:2013-12-30 21:39:34

标签: java android json arraylist

我的问题是,如何将保存的Json Sting转换回自定义ArrayList。

这是我保存ArrayList的方法:

public void saveLocList(ArrayList<LocItem> locList) {
    SharedPreferences.Editor e = returnPref.edit();
    Gson gson = new Gson();
    String json = gson.toJson(locList);
    e.putString(LOCLIST, json);
    e.commit();
}

现在,当我恢复ArrayList(Json String)时,它无法使用此代码:

public ArrayList<LocItem> getLocList() {
    String json = returnPref.getString(LOCLIST, "");
    Type type = (Type) new TypeToken<ArrayList<LocItem>>() {
    }.getType();
    ArrayList<LocItem> inpList = new Gson().fromJson(json, (java.lang.reflect.Type) type);
    return inpList;
}

这是我的日志:

12-30 22:19:19.395: E/AndroidRuntime(2254): FATAL EXCEPTION: main
12-30 22:19:19.395: E/AndroidRuntime(2254): java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackagename.MainActivity}: java.lang.ClassCastException: com.google.gson.internal.$Gson$Types$ParameterizedTypeImpl cannot be cast to android.renderscript.Type
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread.access$600(ActivityThread.java:153)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.os.Looper.loop(Looper.java:137)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread.main(ActivityThread.java:5289)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at java.lang.reflect.Method.invokeNative(Native Method)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at java.lang.reflect.Method.invoke(Method.java:525)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at dalvik.system.NativeStart.main(Native Method)
12-30 22:19:19.395: E/AndroidRuntime(2254): Caused by: java.lang.ClassCastException: com.google.gson.internal.$Gson$Types$ParameterizedTypeImpl cannot be cast to android.renderscript.Type
12-30 22:19:19.395: E/AndroidRuntime(2254):     at mypackagename.helper.Preferences.getLocList(Preferences.java:152)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at mypackagename.MainActivity.onCreate(MainActivity.java:137)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.Activity.performCreate(Activity.java:5133)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
12-30 22:19:19.395: E/AndroidRuntime(2254):     ... 11 more

我不知道要改变什么,也许saveformat有问题(但它有效);

谢谢

1 个答案:

答案 0 :(得分:3)

错误消息告诉您这是一个投射问题。

TypeToken.getType()返回类ParameterizedTypeImpl的对象,该对象实现java.lang.reflect.Type

但是,由于我假设你有android.renderscript.Type的导入声明,Java正试图将ParameterizedTypeImpl强制转换为android.renderscript.Type

只需修复import语句或使用完全限定名称(java.lang.reflect.Type),就像稍后在代码中那样。