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

}

私は何が間違っているのですか?私が渡しているのはSerializableです。

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 に答える