当使用android捆绑包时,为什么序列化堆栈反序列化为ArrayList?

当使用android捆绑包时,为什么序列化堆栈反序列化为ArrayList?,android,serialization,bundle,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

序列化:

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");
Bundle activityArguments=new Bundle();
堆栈它是已知的。我很惊讶它仍然存在

使用通用容器,如:

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

如果您使用
GSON
库,请将堆栈转换为字符串,并将其用作捆绑包的单个字符串,而不进行序列化。它应该会起作用

我希望我不是在胡说八道

堆栈不实现可序列化,而只是扩展了可序列化的
向量
,它相当于
ArrayList

我不知道
等价物的真正定义,也不知道它有多松散,但可以说,可序列化的堆栈的第一个超类是Vector


既然我们看到了这个异常,那么我可能会假设将Serializable转换为ArrayList不应该引发这个异常。否则,我就胡说八道了。

只需将从捆绑包中检索到的可序列化文件转换为列表,创建一个新堆栈,然后将所有列表项添加到堆栈中:

Serializable serializable = savedInstanceState.getSerializable("key");
List<Something> list = (List<Something>) serializable;
Stack<Something> stack = new Stack<Something>();
stack.addAll(list);
Serializable-Serializable=savedInstanceState.getSerializable(“key”);
列表=(列表)可序列化;
堆栈=新堆栈();
stack.addAll(列表);

为什么要投列表而不是你问的ArrayList?因为如果这在某些Android版本中得到修复,您就不会再有ClassCastException了。

谢谢您的回答。最后,我只是使用了一个arraylist,因为它只意味着在读取元素后删除该元素的一行代码。Stack是完成这项工作的合适工具,但arraylist就在眼前!Vector是堆栈的超类,而不是ArrayList。