9

I want to pass a Runnable into an activity via bundle, so that runnable must run when onCreate fires. I wrote a class which implements Serializable but it causes exception: "java.lang.RuntimeException: Parcelable encountered IOException writing serializable object". My code goes here:

package some.company.common;

import java.io.Serializable;

public class SerializedRunnable implements Serializable, Runnable {

    private static final long serialVersionUID = 6641813629033240205L;

    public SerializedRunnable() {
    }

    private Runnable runnable;

    public SerializedRunnable(Runnable runnable) {
        this.runnable = runnable;
    }

    @Override
    public void run() {
        this.runnable.run();
    }

}
Hossein POURAKBAR
  • 1,073
  • 2
  • 15
  • 33
  • 2
    it is just the contents of the object that are serialized. Why do you want to serialize a runnable, which looks to be a waste. you can always pass just the data to another runnable that will take this data and process – nandeesh Dec 16 '12 at 09:34

1 Answers1

0

It looks like Runnable is not serializable. To implement your own serialization you must implement readObject and writeObject by yourself.

Check here

fedepaol
  • 6,834
  • 3
  • 27
  • 34
  • I added two methods just like this: `private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException, IOException { aInputStream.defaultReadObject(); } private void writeObject(ObjectOutputStream aOutputStream) throws IOException { aOutputStream.defaultWriteObject(); }` but the same error continues. – Hossein POURAKBAR Dec 16 '12 at 09:24