Reputation: 5797
I've looked at other questions with the same error but they all seem specific to that user's situation. I was unable to find a solution to my problem.
I'm using gson in my Android app to save a list of my custom object into sharedPreferences. The object is very basic:
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; }
}
The list is declared like this:
private static List<Idea> ideas = new ArrayList<Idea>();
Serialization seems to work OK but when I try to decode it in the MainActivity like so:
String jsonIdeas = sharedPref.getString(IDEAS_KEY, null);
Gson gson = new Gson();
ideas = gson.fromJson(jsonIdeas, ArrayList.class);
I am getting this error:
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)
I don't think the location I'm getting in the stack trace is accurate because it changes whenever I debug the app. But by break pointing and evaluating in IntelliJ I was able to narrow the error down to decoding code I posted above.
Just in case the serialization is actually not OK, here's the code I'm using:
Gson gson = new Gson();
String json = gson.toJson(ideas);
editor.putString(IDEAS_KEY, json); //putting it into sharedPreferences
The gson was working fine when I was using it to store a list of string lists, but when I carried over the code to use with my custom object I started getting this error.
Upvotes: 1
Views: 3532
Reputation: 32026
You need to use a gson TypeToken
. TypeTokens are needed when you are trying to deserialize generic types.
For your case, it would be something like --
Type typeOfListOfIdea = new TypeToken<ArrayList<Idea>>(){}.getType();
ideas = gson.fromJson(jsonIdeas, typeOfListOfIdea);
Your other option is to convert it as an array instead of an ArrayList
.
Idea ideas[] = gson.fromJson(jsonIdeas, Idea[].class);
Then you can load into an ArrayList
(or convert with the Arrays.asList
method.
Upvotes: 8