Well I don't know why the intent is converting your Hashtable to a
HashMap, the basic serialisation process returns the right thing.
You should be able to work around the problem by passing the HashMap
into a new Hashtable instance, however, similar to this:
Hashtable<String, String> table = null;
Serializable data = getIntent().getSerializableExtra("table");
if (data != null) {
table = new Hashtable<String, String>((HashMap<String, String>)data);
}
Not perfect, but should get you what you want.
I suspect there is some lower-level issue with using Hashtable in the
Android internals (this isn't the only time I've heard of a problem
when using Hashtable). I believe the Android platform prefers the use
of the more recent HashMap class - alongside
Collections.synchronizedMap if you need it to be thread-safe.
Cheers,
Sean
On Fri, Nov 13, 2009 at 11:42 AM, Samuh <[email protected]> wrote:
> Sean,
>
> Please don't be sorry. I would be more than glad to hear any advices
> that you have to offer; it might come handy some other time. :)
>
> What you have said is more of a convention and better practice to
> write safe and robust code. I am fine with that.
>
> My main problem is, even if I put a Hashtable in the Intent extras
> using putExtra(Serializable), when I fetch it[using
> getSerializableExtra(..)] I have to cast it to a HashMap and NOT
> Hashtable. If I cast it to a Hashtable, I get a ClassCastException.
>
> I am sure there must be some reason for this which I, unfortunately
> fail to understand.
>
> On Nov 13, 4:19 pm, Sean Hodges <[email protected]> wrote:
>> Sorry, I missed this mail, glad to hear you got it working.
>>
>> On Fri, Nov 13, 2009 at 11:07 AM, Samuh <[email protected]> wrote:
>> >> The ClassCastException means that the Java Runtime does not like your
>> >> request to convert a Serializable into a HashMap.
>>
>> > I understand the meaning of ClassCastException; I did skim through the
>> > Javadocs docs before posting here.
>> > The problem is: I am not casting Serializable to HashMap but a
>> > Hashtable.
>>
>> >> > Hashtable table = (Hashtable)getIntent().getSerializableExtra
>> >> > ("table");
>>
>> > I put Hashtable in the Intent Extras() so I thought the cast was safe.
>>
>> > Thanks.
>>
>> > On Nov 13, 3:38 pm, Indicator Veritatis <[email protected]> wrote:
>> >> The ClassCastException means that the Java Runtime does not like your
>> >> request to convert a Serializable into a HashMap. What is less clear
>> >> is why: but the J2SE API docs give a pretty good clue when they say:
>>
>> >> "Thrown to indicate that the code has attempted to cast an object to a
>> >> subclass of which it is not an instance. "
>> >> (http://java.sun.com/j2se/1.4.2/docs/api/)
>>
>> >> On Nov 13, 1:32 am, Samuh <[email protected]> wrote:
>>
>> >> > I have a Hashtable, which, I need to pass to another activity. Since,
>> >> > Hashtable is Serializable, I am doing the following:
>>
>> >> > Hashtable mHashTable = new Hashtable();
>> >> > // load values
>>
>> >> > Intent intent = new Intent(this,SecondActivity.class);
>> >> > intent.put("table",mHashTable);
>>
>> >> > startActivity(intent);
>>
>> >> > In the Second activity, I try to read the extras in OnCreate(..) as:
>>
>> >> > Hashtable table = (Hashtable)getIntent().getSerializableExtra
>> >> > ("table");
>>
>> >> > I get ClassCastException at this line in Logcat:
>>
>> >> > 11-13 14:21:15.939: ERROR/AndroidRuntime(922): Caused by:
>> >> > java.lang.ClassCastException: java.util.HashMap
>>
>> >> > What is that I am missing or doing wrong here?
>>
>> >> > Please help.
>>
>> >> > Thanks.- Hide quoted text -
>>
>> >> - Show quoted text -
>>
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "Android Beginners" group.
>> > To post to this group, send email to [email protected]
>> > To unsubscribe from this group, send email to
>> > [email protected]
>> > For more options, visit this group at
>> >http://groups.google.com/group/android-beginners?hl=en
>>
>>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Beginners" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-beginners?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en