Classcastexception When Retrieving Data From Bundle On Android
Solution 1:
Interesting read concerning a similar question can be found here
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).
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:
privateLinkedHashMap<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 hashmapMap<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);
}
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.
Solution 2:
The problem is that the reference returned simply isn't a reference to an instance of LinkedHashMap
. If function Return LinkedHashMap
just returns a LinkedHashMap
, it can't be cast to HashMap.
Post a Comment for "Classcastexception When Retrieving Data From Bundle On Android"