3

例外:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = MyClass$2)

(简化的)代码:

i.putExtra("myparam", generateA(context, "foo"));
...
private A generateA(final Context context, String foo) {
    return new A() {

        @Override
        public void test() {
            System.out.println("test");
        }
    };
}

界面:

public interface A extends Serializable {

    public void test();

}

我究竟做错了什么?我传递的是一个可序列化的。

4

1 回答 1

0

如果我理解得很好,意图失败是因为您的匿名类由于其处理程序而无法序列化?

是否可以在目标活动中实现您的处理程序?bahaviour 类应该只包含行为函数和目标活动提供的处理程序:

private transient Handler h;

//Called by the activity in the onCreate
void setHandler(Handler h){
   this.h = h;
}

//Called by the activity to start the behaviour function
void startBehaviour (){
  //...
  //The activity handler will call startHandlerBehaviour itself
  h.sendMessage();
}

//Called by the activity in the Handler to execute in the good context
void startHandlerBehaviour() {
  //...
}
于 2014-02-19T09:42:32.600 回答