2 annbnncnnd AnnBnnCnnD 于 2012.12.21 11:03 提问

为什么一个序列化的栈能反序列化成一个ArrayList

序列化:

Bundle activityArguments = new Bundle();
Stack<Class<? extends WizardStep>> wizardSteps = new Stack<Class<? extends WizardStep>>();
        wizardSteps.push(CreateAlarmStep5View.class);
        wizardSteps.push(CreateAlarmStep4View.class);
        wizardSteps.push(CreateAlarmStep3View.class);
        wizardSteps.push(CreateAlarmStep2View.class);
        wizardSteps.push(CreateAlarmStep1View.class);

        activityArguments.putSerializable("WizardSteps", wizardSteps);

反序列化:

Stack<Class<? extends WizardStep>> wizardSteps = 
(Stack<Class<? extends WizardStep>>) getIntent().getExtras().getSerializable("WizardSteps");

异常:

12-20 23:19:45.698: E/AndroidRuntime(12145): Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Stack

1个回答

ce_la_vie
ce_la_vie   2012.12.21 11:40
已采纳

这是一个bug。

使用通用的container:

public class SerializableHolder implements Serializable {
private Serializable content;
public Serializable get() {
    return content;
}
public SerializableHolder(Serializable content) {
    this.content = content;
 }
}

如果用GSON库, 转换栈为String 并且使用没有序列化的单个字符串包

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!