我正在尝试在其具有接口的构造函数中序列化一个对象,我将解释:
这是Object类:
public class MyObj implements Serializable{
private static final long serialVersionUID = -29238982928391L;
private MyCallback onMyCallback;
private String shortcutsText;
public MyObj(String shortcutsText, MyCallback onMyCallback){
this.shortcutsText = shortcutsText;
this.onMyCallback = onMyCallback;
}
public MyCallback getOnMyCallback() {
return onMyCallback;
}
}
这是MyCallback接口:
public interface MyCallback extends Serializable {
long serialVersionUID = -1306760703066967345L;
void onMyCallbackClickListener();
}
但是当我尝试用(
out
是ObjectOutputStream
)编写它时:MyCallback myCallback = new MyCallback() {
@Override
public void onMyCallbackClickListener() {
Toast.makeText(getApplicationContext(), "TEST!", Toast.LENGTH_LONG).show();
}
};
out.writeObject(myCallback);
或搭配:
out.writeObject(new MyObj("Test", myCallback));
它抛出:
java.io.NotSerializableException
最佳答案
匿名内部类不可序列化。在此处查看良好的解释:NotSerializableException on anonymous class