title: Can't cast gson to my Object type
body: <p>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.</p>
  
  <p>I'm using gson in my Android app to save a list of my custom object into sharedPreferences. The object is very basic:</p>
  
  <pre><code>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; }
  }
  </code></pre>
  
  <p>The list is declared like this:</p>
  
  <pre><code>private static List&lt;Idea&gt; ideas = new ArrayList&lt;Idea&gt;();
  </code></pre>
  
  <p>Serialization seems to work OK but when I try to decode it in the MainActivity like so:</p>
  
  <pre><code>String jsonIdeas = sharedPref.getString(IDEAS_KEY, null);
  Gson gson = new Gson();
  ideas = gson.fromJson(jsonIdeas, ArrayList.class);
  </code></pre>
  
  <p>I am getting this error:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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. </p>
  
  <p>Just in case the serialization is actually not OK, here's the code I'm using:</p>
  
  <pre><code>Gson gson = new Gson();
  String json = gson.toJson(ideas);
  editor.putString(IDEAS_KEY, json); //putting it into sharedPreferences
  </code></pre>
  
  <p>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.</p>