如何将runnable放入bundle?

时间:2012-12-16 08:41:00

标签: android runnable serializable

我想通过bundle将Runnable传递给一个activity,这样runnable必须在onCreate触发时运行。我编写了一个实现Serializable的类,但它导致异常:"java.lang.RuntimeException: Parcelable encountered IOException writing serializable object"。 我的代码在这里:

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();
    }

}

1 个答案:

答案 0 :(得分:0)

看起来Runnable不可序列化。要实现自己的序列化,您必须自己实施readObjectwriteObject

检查here