-1

I have a class that implements Serializable and has a following structure:

public class WorkWishesForm implements Serializable {

    //================================================================================
    // Переменные
    //================================================================================
    public ArrayList<OfferTrade> offerTradeArray;
    public int salary;
    public OperatingSchedule operatingSchedule;
    public boolean businessTripsAllowed;
    public ArrayList<Region> regionArray;


    //================================================================================
    // Конструктор
    //================================================================================
    public WorkWishesForm() {

    }
}

Class OperatingSchedule also implements Serializable, but it's state is not saved for some reason after serialization of WorkWishesFormclass. This is it's structure:

public class OperatingSchedule extends NamedObject implements Serializable {

    //================================================================================
    // Конструктор
    //================================================================================
    public OperatingSchedule(JSONObject jsonObject, Context context) {
        super(context);

        try {
            name = jsonObject.getString("name");
            id = jsonObject.getInt("id");
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public OperatingSchedule() {
        super();
    }


    //================================================================================
    // Другое
    //================================================================================
    public String toString()
    {
        return this.name;
    }
}

And here is NamedObject:

public class NamedObject {
    //================================================================================
    // Переменные
    //================================================================================
    protected int id;
    protected String name;
    protected Context context; //Контекст часто бывает нужен для обращения к ресурсам, например, строкам


    //================================================================================
    // Конструкторы
    //================================================================================
    public NamedObject (Context context) {
        this.context = context;
    }


    public NamedObject() {

    }


    //================================================================================
    // Геттеры и сеттеры
    //================================================================================
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    //================================================================================
    // Другое
    //================================================================================

    public static ArrayList<String> stringListFromObjectList (ArrayList<NamedObject> objectList)
    {
        ArrayList<String> stringList = new ArrayList<String>();
        for (NamedObject obj : objectList) {
            stringList.add(obj.getName());
        }

        return stringList;
    }

}

If I implement Serializable in NamedObject, class WorkWishesForm becomes completely Unserializable (error occurs). When I don't implement, the state of OperatingSchedule is not saved.

What should I do, to make it save?

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70

1 Answers1

0
public class NamedObject implements Serializable {
       transient protected Context context; 
       ...

I made three things to make it work:

  1. Made Context a transient field.
  2. Made NamedObject implement Serializable.
  3. Fixed an error in my code, where I was setting up OperatingSchedule to the form. This error doesn't concern Serialization troubles, it was my personal mistake in another part of code. Actually, when I did first two things, it started to save a state, I just didn't use it correctly.

One more thing : added serialVersionUID to every class that implements Serializable. In Android Studio used a plugin GenerateSerialVersionUID.

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70