How to use gson to serialize classes with custom object attributes in android?
<p>I want to use Gson library to serialize and store an object which is of a custom class named "Battery". In the Battery class I have defined an array of a another custom object which is "Cannon". Here are my two classes:</p>

<p>Cannon.java:</p>

<pre><code>import android.widget.ImageView;

public class Cannon {
    private String id; // used for object storage
    private int number; // Cannon number in battery
    private ImageView icon;

    private long utmEast, utmNorth; // Used to define cannon global location
    private boolean isBase; // Defines if the cannon is base

    private double flyingTime;
    private int angle, side;

    public Cannon() {

    }

    public Cannon(String id, int number, ImageView icon, String connectionNumber){
        this.id = id;
        this.number = number;
        this.icon = icon;
        this.connectionNumber = connectionNumber;
    }

    public String getId() {
        return this.id;
    }

    public int getNumber() {
        return this.number;
    }

    public ImageView getIcon() {
        return this.icon;
    }

    public long getUtmNorth() {
        return utmNorth;
    }

    public void setUtmNorth(long utmNorth) {
        this.utmNorth = utmNorth;
    }

    public long getUtmEast() {
        return utmEast;
    }

    public void setUtmEast(long utmEast) {
        this.utmEast = utmEast;
    }

    public void setAsBase(boolean isBase) {
        this.isBase = isBase;
    }

    public boolean isBaseCannon() {
        return isBase;
    }
}
</code></pre>

<p>Battery.java:</p>

<pre><code>public class Battery {

    private String id; // used for object storage

    private final Cannon cannons[] = new Cannon[6];

    public Battery(String id){
        this.id = id;
    }

    public String getId() {
        return this.id;
    }

    public Cannon[] getCannons() {
        return cannons;
    }

    public void setCannon(Cannon cannon, int position) {
        this.getCannons()[position] = cannon;
    }

    public Cannon getCannon(String cannonId) {
        for (Cannon cn: this.getCannons()) {
            if (cn.getId() == cannonId) {
                return cn;
            }
        }
        return null;
    }

    public Cannon getCannon(int position) {
        return getCannons()[position];
    }
}
</code></pre>

<p>and here's my Preferences class which uses shared preferences to store application specific fields including battery object using Gson library.</p>

<pre><code>import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;

import net.adeveloper.firecontrol.data.Battery;

/** stores the user object in SharedPreferences */
public class Preferences {
    /** This application's preferences label */
    private static final String PREFS_NAME = "net.adeveloper.firecontrol.preferences";
    /** This application's preferences */
    private static SharedPreferences settings;
    /** This application's settings editor*/
    private static SharedPreferences.Editor editor;
    /** Constructor takes an android.content.Context argument*/
    private final Gson gson = new Gson();

    /**************************************************/

    private final Battery battery = new Battery(Reference.BATTERY_ID);

    /**************************************************/

    public Preferences(Context ctx){
        if(settings == null){
            settings = ctx.getSharedPreferences(PREFS_NAME,
                    Context.MODE_PRIVATE );
        }
       /*
        * Get a SharedPreferences editor instance.
        * SharedPreferences ensures that updates are atomic
        * and non-concurrent
        */
        editor = settings.edit();

        // Initialize battery
        String battery_json = gson.toJson(battery);
        // store in SharedPreferences
        String id = battery.getId(); // get storage key
        editor.putString(id, battery_json);
        editor.commit();
    }

    public Battery getBattery() {
        String battery_json = settings.getString(Reference.BATTERY_ID, "");
        Battery battery = gson.fromJson(battery_json, Battery.class);
        return battery;
    }

    //...

}
</code></pre>

<p>I use the preference object in my MainActivity to store application fields:</p>

<pre><code>@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //...

        /********************************************************/

        prefs = new Preferences(this.getApplicationContext());

        /********************************************************/
</code></pre>

<p>However when it launches the application crashes and outputs Gson stackoverflowError:</p>

<pre><code>02-10 16:39:03.110 13187-13187/net.adeveloper.firecontrol E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            java.lang.StackOverflowError
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
                                                                                at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:355)
                                                                                at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:117)
                                                                                at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
                                                                                at com.google.gson.Gson.getAdapter(Gson.java:356)
                                                                                at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.&lt;init&gt;(ReflectiveTypeAdapterFactory.java:82)
                                                                                at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
                                                                                at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
                                                                                at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
                                                                                at com.google.gson.Gson.getAdapter(Gson.java:356)
                                                                                at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.&lt;init&gt;(ReflectiveTypeAdapterFactory.java:82)
                                                                                at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.cr
</code></pre>

<p><strong>Update:</strong></p>

<p>I added the this code to my MainActivity after setContentView as I was using a device with an android version of 4.2.1:</p>

<pre><code>@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

if (android.os.Build.VERSION.SDK_INT &gt; 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

//...

                /********************************************************/

                prefs = new Preferences(this.getApplicationContext());

                /********************************************************/
</code></pre>

<p>And now the stackoverflow exception has gone, but now another problem arose which is my Json string returned by toJson() and fromJson() methods is null and when I set my cannon object through the battery class using Preference object actually cannot access it again and it returns null:</p>

<pre><code>Cannon cannon = new Cannon(id, number, icon, phone);
                prefs.getBattery().setCannon(cannon, position);
                if (prefs.getBattery().getCannons() == null) {
                    Log.d("Cannon", "NULL");
                } else {
                    for (Cannon cn: prefs.getBattery().getCannons()) {
                        Log.d("Cannon", cn + "");
                    }
                }
</code></pre>

<p>outputs:</p>

<pre><code>Cannon: null
Cannon: null
Cannon: null
Cannon: null
Cannon: null
Cannon: null
</code></pre>