在Android上从bundle检索数据时发生ClassCastException
我想在片段的生命周期中保存一些状态。例如,当屏幕旋转时,它可以正常工作,但当进程被终止并从磁盘恢复时(我认为它就是这样工作的),我会得到一个ClassCastException。 下面是一些代码: 初始化:在Android上从bundle检索数据时发生ClassCastException,android,serialization,bundle,Android,Serialization,Bundle,我想在片段的生命周期中保存一些状态。例如,当屏幕旋转时,它可以正常工作,但当进程被终止并从磁盘恢复时(我认为它就是这样工作的),我会得到一个ClassCastException。 下面是一些代码: 初始化: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { playlistsM
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
playlistsMap = new LinkedHashMap<Section, List<Playlist>>();
} else {
playlistsMap = (LinkedHashMap<Section, List<Playlist>>) savedInstanceState.getSerializable(PLAYLISTS_MAP_KEY);
}
setHasOptionsMenu(true);
}
我知道在安卓系统上使用包裹更好,但我仍然不明白这是怎么发生的
有什么想法吗?问题是返回的引用根本不是对
LinkedHashMap
实例的引用。如果函数ReturnLinkedHashMap
仅返回一个LinkedHashMap
,则无法将其转换为HashMap。可以找到关于类似问题的有趣读物
任何同时实现java.util.List和java.io.Serializable的对象都将成为
intent.putExtra(EXTRA_标记,
suchObject)/startActivity(intent)/intent.getSerializableExtra(EXTRA_标记)
我敢说,对于实现serializable和map的任何东西来说,这同样重要。该HashMap是您将返回的默认值
围绕这一问题的解决方案类似于一个bug:
private LinkedHashMap<Section, List<Playlist>> playlistsMap;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
playlistsMap = new LinkedHashMap<Section, List<Playlist>>();
} else {
//assuming the bug returns a hashmap
Map<Section, List<Playlist>> items = (Map<Section, List<Playlist>>) savedInstanceState.getSerializable(PLAYLISTS_MAP_KEY);
//this way you can ensure you are working with a linkedHashMap.
//for the rest off the application
playlistsMap.putAll(items);
}
setHasOptionsMenu(true);
}
private LinkedHashMap playlistmap;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(savedInstanceState==null){
playlistmap=newlinkedhashmap();
}否则{
//假设bug返回一个hashmap
Map items=(Map)savedInstanceState.getSerializable(播放列表\映射\键);
//通过这种方式,您可以确保您使用的是linkedHashMap。
//对于应用程序的其余部分
playlistmap.putAll(项目);
}
设置选项菜单(真);
}
上面的代码没有经过测试,但应该可以工作。
当由于使用接口而修复错误时,此代码仍然有效。
因此,当您的客户端获得android更新时,您可以放心,您的代码仍应执行相同的操作,而不是崩溃并抱怨无法将哈希映射转换为LinkedHashMap。我知道classcastexception的含义,我只是不知道它来自何方。我正在将LinkedHashMap作为可序列化的对象放入捆绑包中,我希望bundle.getSerializable返回相同的对象。检查从何处获取LinkedHashMap并将其传递到HashMap。是的,我知道LinkedHashMap和HashMap不能互换,我只是不知道HashMap来自何处。putSerializable/getSerializable方法不需要知道我在说什么,因为它们实现了Serializable…嗯,可能是android库本身的一个bug。我读过很多和你有类似问题的人。不过,我还没有找到“为什么”的答案。如果我找到更多,我会继续找,并告诉你。对于临时解决方案,可以将其转换为映射。这应该行得通,如果它抱怨使用HashMapEdited来转换我的答案并带有链接,那可能是一个有趣的阅读。好的,谢谢你的链接!对于这样一个普通的任务来说,这是一个奇怪的行为。。。你能用相关信息编辑你的帖子吗,这样我就接受了,你就可以收获你的观点了?@康普茶编辑了我的答案,并添加了一个修复示例,希望能帮助你或其他有类似问题的人解决问题。在我的例子中,我在整个savedInstanceState中保存了哈希表。当我旋转屏幕时,我们会得到哈希表,但万一应用程序崩溃,正如您所提到的,我们会得到一个哈希映射,因此会出现类强制转换异常。您是否找到了任何好的解决方案或解决方法?
04-10 01:06:43.430 E/AndroidRuntime(28549): FATAL EXCEPTION: main
04-10 01:06:43.430 E/AndroidRuntime(28549): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.android/com.mydomain.android.ui.MainActivity}:
java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.LinkedHashMap
private LinkedHashMap<Section, List<Playlist>> playlistsMap;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
playlistsMap = new LinkedHashMap<Section, List<Playlist>>();
} else {
//assuming the bug returns a hashmap
Map<Section, List<Playlist>> items = (Map<Section, List<Playlist>>) savedInstanceState.getSerializable(PLAYLISTS_MAP_KEY);
//this way you can ensure you are working with a linkedHashMap.
//for the rest off the application
playlistsMap.putAll(items);
}
setHasOptionsMenu(true);
}