简体   繁体   中英

Java RuntimeException for serializable object with Serializable and Custom Interface

I am encountering the following issue:

Let's say I have a RiceContainerClass like so:

public class RiceContainerClass implements Serializable{
 private typeOfRice
 private countryOfOrigin

 ...

I am able extend this class, (maybe I want some SteamedRiceClass ), but most importantly I am able to pass this class between Activities with my current implementation.

The issue arose when I decided that my Rice class might need to come with a Sauceable Interface.

I wrote the interface:

public interface Sauceable{

  public void setTypeOfSauce(Sauce sc)

  public Sauce getRiceSauce()
}

(In my program the Sauce is actually a Drawable )

When I modified my RiceContainerClass to implement the Sauce Interface:

public class RiceContainerClass implements Serializable, Sauceable

I get the following error during runtime from the logcat:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.app.RiceContainerClass)

Any advice on what to do? I tried making the Sauce class extend Serializable , thought maybe that would work to no avail.

That means that app.RiceContainerClass is not Serializable .

Just make sure that this class implements the Serializable interface and define the methods to write and read the Object.

You can check some examples here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM