I am facing an issue while passing a LinkedHashMap from one Activity to another. I referred all the related post by none of them could solve my problem. Please help me out.
Activity 1:
Intent mapIntent = new Intent(this,GMap.class);
LinkedHashMap<TravelMode, String> polyPoints=(LinkedHashMap<TravelMode, String>) gData.values().toArray()[0];
mapIntent.putExtra(EXTRA_MESSAGE, polyPoints);
startActivity(mapIntent);
Activity 2:
LinkedHashMap<Object,String>polypoint = (LinkedHashMap<Object, String>)poly.getSerializableExtra(EXTRA_MESSAGE);
This is the error I m getting while doing this operation.
Error:
ClassCastException: Cannot cast java.util.HashMap (id=830032266720) to java.util.LinkedHashMap
Class TravelMode:
class TravelMode implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public String travelMode;
/**
* @return the travelMode
*/
public String getTravelMode() {
return travelMode;
}
/**
* @param travelMode the travelMode to set
*/
public void setTravelMode(String travelMode) {
this.travelMode = travelMode;
}
public TravelMode(String travelMode) {
super();
this.travelMode = travelMode;
}
}
I tried all the possibilities of retrieving like this one below, But still I getting the same error :(
HashMap<?,?>hashPoly= (HashMap<?, ?>)poly.getSerializableExtra(EXTRA_MESSAGE);
LinkedHashMap<TravelMode, String> polypoint= ((LinkedHashMap<TravelMode, String>)hashPoly);
You can't directly cast from a HashMap
to a LinkedHashMap
. You should be able to do this instead:
LinkedHashMap<Object,String>polypoint = new LinkedHashMap<Object, String>();
and then add the previous HashMap
with putAll
:
polypoint.putAll(polyPoints);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With