ClassCastException异常的捆绑在Android上检索数据时 [英] ClassCastException when retrieving data from bundle on Android

查看:180
本文介绍了ClassCastException异常的捆绑在Android上检索数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些状态,我想保存翻过一个片段的生命周期。它工作正常,当屏幕旋转时的例子,但在过程中已经死亡,从磁盘恢复(我认为这是它是如何工作),我得到一个ClassCastException。 下面是一些code:

I have some state that I want to save accross the lifecycle of a fragment. It works fine when the screen rotates for example, but when the process has been killed and restored from disk (I think that's how it works), I get a ClassCastException. Here's some code :

初​​始化:

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

保存数据:

@Override
    public void onSaveInstanceState(Bundle outState) {
        if (isEverySectionsLoaded()) {
            outState.putSerializable(PLAYLISTS_MAP_KEY, playlistsMap);
        } else {
            outState.putSerializable(PLAYLISTS_MAP_KEY, new LinkedHashMap<Section, List<Playlist>>());
        }
// ...

我从剧组得到的异常在的onCreate

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

我知道这是最好使用parcelables的Andr​​oid上,但我还是不明白怎么会不会发生。

I know it's better to use parcelables on Android, but I still don't understand how that could ever happen.

任何想法?

推荐答案

有关类似的问题,有趣的阅读可以发现的这里

Interesting read concerning a similar question can be found here

这是同时实现的java.util.List和java.io.Serializable的将成为任何对象   ArrayList的intent.putExtra(EXTRA_TAG后,   suchObject)/ startActivity(意向)/intent.getSerializableExtra(EXTRA_TAG)。

Any object that implements both java.util.List and java.io.Serializable will become ArrayList after intent.putExtra(EXTRA_TAG, suchObject)/startActivity(intent)/intent.getSerializableExtra(EXTRA_TAG).

我不敢说,对于任何事情一样计数实现序列化和地图。这HashMap的是默认的,你会回来。

I dare to say that the same counts for anything that implements serializable and map. That HashMap is the default you will get back.

对此的一种解决方案是什么似乎是一个错误是类似于:

A solution around this what seems like a bug would be something similar to:

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

以上code未经测试,但应该工作。 当错误被固定由于界面的使用这code仍然可以工作。 所以,你可以休息的时候你的客户端获取一个Android更新您的code还是应该做的,而不是崩溃,并抱怨说,你不能施放一个HashMap来LinkedHashMap中同样有保证。

The above code isn't tested but should work. This code will still work when the bug gets fixed due to the usage of interfaces. So you can rest assured when your client gets an android update that your code should still do the same instead of crashing and complaining that you cant cast a HashMap to a LinkedHashMap.

这篇关于ClassCastException异常的捆绑在Android上检索数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆