gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:03:38 24 4
gpt4 key购买 nike

序列化:

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/

24 4 0
文章推荐: java - 容器的投影,即将 List 转换为 List 的方法