0

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

aivision2020
  • 619
  • 6
  • 14
  • Did you read this ? http://stackoverflow.com/questions/13960153/putextra-treemap-returns-hashmap-cannot-be-cast-to-treemap-android – Alexis C. May 21 '13 at 13:47
  • Thanks, Also Vector is coming out as ArrayList. I don't mind using the other classes but it's strange. and why is it getting screwed up only between Activitied and not when it goes through OnResume? Thanks. – aivision2020 May 22 '13 at 12:07
  • try this one, this one will definitely help you. http://stackoverflow.com/questions/134492/how-to-serialize-an-object-into-a-string – Syed_Adeel May 22 '13 at 13:19
  • That seems a bit over the top. Serialization should be simple (imo). Anyway, I've moved on, and will use HashMap and ArrayList. no more problems. I've made my peace with it – aivision2020 May 24 '13 at 20:19

0 Answers0