android - 使用 android bundle 时,为什么序列化程序堆栈反序列化为 ArrayList?

标签 android serialization bundle

序列化:

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");

异常(exception):

12-20 23:19:45.698: E/AndroidRuntime(12145): Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to 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 并在没有 Serialize 的情况下将其用作 Bundle 的单个 String。它应该工作。

关于android - 使用 android bundle 时,为什么序列化程序堆栈反序列化为 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982192/

相关文章:

java - Android指纹取消信号FINGERPRINT_ACQUIRED_TOO_FAST

java - SonarQube - "Serializable"类中的规则字段应该是 transient 的或可序列化的

iphone - Appstore上传问题

android - 在将数据从 fragment 传递到 Activity 期间获得 Null 值

android - 单击 Android 通知操作不会关闭通知抽屉

android - 如何在不更改标准操作栏的情况下更改上下文操作栏的溢出图标?

serialization - Erlang序列化

json - Newtonsoft.json IsoDateTimeConverter 和 DateFormatHandling

java - 在 OSGi 包中使用常规 java 类

Android嵌套线性布局