使用android包时,为什么序列化堆栈反序列化为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);

Deserialisation:

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

例外:

12-20 23:19:45.698:E / AndroidRuntime(12145):引起:java.lang.ClassCastException:java.util.ArrayList无法强制转换为java.util.Stack

解决方法:

它的知名度为bug.我很惊讶它仍然存在.

使用通用容器,如:

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

如果您使用GSON库,请将Stack转换为String并将其用作Bundle的单个String,而不使用Serialize.它应该工作.

上一篇:280 Android基本布局之2—帧布局


下一篇:Android Crash Learning