Mellon
Mellon

Reputation: 38912

Write Parcelable object got java.io.NotSerializableException

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?

Upvotes: 3

Views: 2438

Answers (2)

Jarvis
Jarvis

Reputation: 1547

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

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157487

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.

Upvotes: 3

Related Questions