My qestion is, how I can convert an saved Json Sting back to an custom ArrayList.
This is how I save my 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();
}
And now, when I restore the ArrayList (Json String) it doesn't work with this code:
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;
}
This is my log:
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
I don't know what to change, maybe there is a problem with the savingformat (but it works);
Thanks
The error message is telling you it's a problem with casting.
TypeToken.getType()
returns an object of class ParameterizedTypeImpl
, which implements java.lang.reflect.Type
.
However, because of an import statement that I'm assuming you have for android.renderscript.Type
, Java is trying to cast the ParameterizedTypeImpl
to an android.renderscript.Type
.
Simply fix the import statement or use the fully qualified name ( java.lang.reflect.Type
) as you do later in the code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.