微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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.它应该工作.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐