1
我試圖序列化一個摘要並將其傳遞給一個活動,但我得到錯誤java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.MainActivity$1)
。序列化抽象類的問題
MainActivity.java
public class MainActivity extends Activity {
private static final String AUTO_EXTRA = "AUTO_EXTRA";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout l = new RelativeLayout(this);
l.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
new Setup.auto(this) {
@Override
public void onFailure(String message) {
}
};
}
}
Setup.java
public static class Setup {
public static abstract class auto implements FailedAuto, Serializable {
public auto(Activity activity) {
auto a = (auto) this;
Intent i = new Intent(activity, AutoSetup.class);
i.putExtra(AUTO_EXTRA, a); // Error happens here.
activity.startActivity(i);
}
}
private class AutoSetup extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((auto) getIntent().getSerializableExtra(AUTO_EXTRA)).onFailure("Message");
}
}
private interface FailedAuto {
void onFailure(String message);
}
}
我有兩個班,MainActivity和設置,當我運行代碼,我得到的線i.putExtra(AUTO_EXTRA, a);
錯誤。我發現序列化MainActivity解決了這個問題,但我不想MainActivity是Serializable
,我不明白爲什麼它認爲我在序列化MainActivity。我在這裏做錯了什麼?
你能解釋一下我的基類的子類是什麼意思嗎?我不清楚你的意思。 –
我的意思是你的抽象類。創建另一個新類,如下所示: public class AutoImpl extends auto { // implementation } –