I'm trying to pass a TreeMap between one activity and another. The TreeMap is of type TreeMap>
I tried making Trip Serializable and Parcelable but I can't get it to work. I also add the map to bundle during onSaveInstanceState and resume it successfully during onRestoreInstanceState but not between activities.
Here is the definition of Trip
package stations;
import java.io.IOException;
import java.io.Serializable;
import android.text.format.Time;
public class Trip implements Serializable {
private static final long serialVersionUID = 1L;
Time m_time = new Time();
public Trip()
{
m_time.setToNow();
}
private void writeObject(java.io.ObjectOutputStream out)
throws IOException {
out.writeLong(m_time.toMillis(false));
}
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException {
m_time = new Time();
m_time.set(in.readLong());
}
}
this part works well:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable(PERSONAL_SET_STR, m_personalSet);
super.onSaveInstanceState(savedInstanceState);
}
@SuppressWarnings("unchecked")
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
m_personalSet = (TreeMap<Integer,Vector<Trip>>)savedInstanceState.getSerializable(PERSONAL_SET_STR);
Toast.makeText(this, "on Restore " + m_personalSet.size(), Toast.LENGTH_SHORT).show();
}
here is how I start the second Activity
Intent intent = new Intent(this, SecondActivity.class);
Bundle b = new Bundle();
b.putSerializable(PERSONAL_SET_STR, m_personalSet);
intent.putExtras(b);
startActivity(intent);
here is the OnCreate of the SecondActivity
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
//This next line throws an exception
m_personalSet = (TreeMap<Integer,Vector<Trip>>)b.getSerializable(FirstActivity.PERSONAL_SET_STR);
}
the Log Cat: 05-21 16:34:24.086: E/AndroidRuntime(17343): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.atme/analysis.SecondClass}: java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.TreeMap
Any ideas why it's comming out of the Bundle as HashMap and not TreeMap? and why only between two activities?
Thanks