3

I have a class named Shop, which has 2 fields and 1 static field. The class implements Parcelable interface:

public class Shop implements Parcelable{
   private static int SHOP_ID = 12;

   private String name;
   private long fund;

   //constructor with parcel
   public Shop(Parcel parcel){
       name = parcel.readString();
       fund = parcel.readLong();
   }

   //normal constructor
   public Shop(Owner owner){
        name = owner.getShopName();
        fund = owner.getFund();
    }

    @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
      dest.writeString(name);
      dest.writeLong(fund);
    }

    public static final Creator<Shop> CREATOR = new Creator<Shop>(){

       @Override
       public Shop createFromParcel(Parcel parcel) {
        //use the constructor which accept parcel
        return new Shop(parcel);
       }

       @Override
       public Shop[] newArray(int size) {
        return new Shop[size];
       }

    };
}

Now, my code initiate a Shop instance by using the normal constructor:

Owner owner = getOwnerInfo();
Shop myShop = new Shop(owner); //initiate a Shop with owner

Then , my code store the shop instance to internal storage of Android:

String fileName = "shop_file";
try{
  FileOutputStream fos = activity.openFileOutput(fileName,Context.MODE_PRIVATE);
  ObjectOutputStream oos = new ObjectOutputStream(fos);         

  oos.writeObject(myShop); 
  oos.flush();
  oos.close();
}catch(Exception ex){
  ...
}

But when run my app, I got java.io.NotSerializableException :

06-12 13:04:29.258: W/System.err(2632): java.io.NotSerializableException: com.my.app.model.Shop
06-12 13:04:29.258: W/System.err(2632):     at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
06-12 13:04:29.266: W/System.err(2632):     at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
06-12 13:04:29.266: W/System.err(2632):     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)

Why? where am I wrong?

Mellon
  • 37,586
  • 78
  • 186
  • 264
  • You try to use Java's own serialization mechanism (using Serializable interface) which is different from Android's parcel-mechanism. – Michael Butscher Jun 12 '13 at 10:30
  • I don't understand you, I am using Parcelable, why you said I am not?? For what I understood, Parcelable is an Android version Serializable interface. – Mellon Jun 12 '13 at 10:34

2 Answers2

3

From the execption seems you are trying to serialize an instance of CartData that does not implements Serializable:

java.io.NotSerializableException: com.my.app.model.Shop

If you want to serialize it you should let Shop implemensts Serializable

From the doc

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Hi, That is my typo. I have updated my post, please check again – Mellon Jun 12 '13 at 10:33
  • still my answer does not change. You have to let shop implements both Serializable and Parcelable if you want to get both – Blackbelt Jun 12 '13 at 10:34
  • Isn't Parcelable a Android specific Serializable interface ? Why I need to implements both ?? – Mellon Jun 12 '13 at 10:35
  • still to use write object with ObjectOutputStream you have to implements Serializable – Blackbelt Jun 12 '13 at 10:37
  • Ok, so, you mean Parcelable object is not supported by ObjectOutputStream as a special version of serializable. By the way, could you please update the Exception text in your post, it is different than my post, which may confuse other visitors. – Mellon Jun 12 '13 at 10:39
  • Thanks blackbelt, by the way, if in my Shop class, I have another field which is a reference to another Object, e.g. Car car, should the Car class also implements serializable in order to use persistent storage on my shop instance? – Mellon Jun 12 '13 at 10:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31644/discussion-between-mellon-and-blackbelt) – Mellon Jun 12 '13 at 10:47
  • yes, all the fields are to be serializable. You can mark a field as transient if you want not to serialize it – Blackbelt Jun 12 '13 at 10:47
0

I think the problem is from your "Owner" object which is not serialized too. try to serialize this object with the Parcelable

Jarvis
  • 1,527
  • 12
  • 17