• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing my object to one to other activity

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I want to pass my object to one activity to other.
eg. I have class myClass which holds sting array, integer, Vector, Hash.
Now I have activity A which set the myClass which stored through json parsing and that object pass to Activity B then B to C.
So how can I pass the object of myClass from activity A to B?
putExtra() working for String, integer only.
I dont have only the myClass object to pass , I may have to pass other object also with it.

Please help.

Thanks,
vina
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use putExtra(Serializable..) and getSerializableExtra() methods to pass and retrieve objects of your class type; you will have to mark your class Serializable and make sure that all your member variables are serializable too...

 
vina parmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Monu.
First it was giving me error then after restarting it's working fine.
You are really helping a lot.
 
vina parmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I serialize the Hashtable to pass it?
I can pass myClass but not able to pass Hashtable which is in other class.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I can pass myClass but not able to pass Hashtable which is in other class


I am not sure I understand what you are saying.

Hashtable IS Serializable.
 
vina parmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also cofused.
My code is



It will get the object of myClass but giving error at hashtable
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am not sure whether i can understand your question

i will give this however

Not all types are serializable.....

check with the api specification whether the java class you have used can be serializable....

If your class uses the file object ,then even though the user defined class is serialized and passed the file object will not pass .....

so make sure the hashtable is serializable

i hope this answer is okay

 
vina parmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok
Let me clarify

I have to pass Hashtable, and other one is myClass which contains Vectors (myClass is Serializable no file class simple vector in myClass).

Now when I passing the object of hashtable and myClass obj Activity A to B, then it will show me the value of myClass and its vectors but giving me error for hashtable.

And How can I know Hashtable is Serializable?

Thanks
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First to know whether a particular java class is serialized(predefined java class like collections,swing and others) type the class name + api in google.It will take you to the java api page where it will have a name called All implemented Interfaces :see whether it has serializable in it .

Example
For hashtable use this link:http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html

are you passing the intent object from one system to another..if yes it must also be serialized....

Actually what is activity A and activity B

For my college project i spent nearly three months on the serialization problem alone

If your myclass is working then the hashtable must also work

I have the following questions
1)what is activity A and B
2) check whether you have overloaded get methods in bundle......
3)you have not given any information about the bundle class and intent class

 
vina parmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply

OK even I am not able to understand your que.
Let me explain again.
Activity Class A contains EditText, Spinner and Button. On Click event of Button it parse json and put the values into the Vectors this will done in myClass which is serialized.
After that as per records I am putting values in the Hashtable which is related to Vectors.
So i have to pass both to the Activity Class B which will display them.

I am not overloading get method.
I am not using Bundle and Intent class more then the given code.

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried sending Hashtable using put(Serializable) and getSerializableExtra(..); I am getting ClassCastExceptions..
 
vina parmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

It is giving me error as : Error in B(304): java.lang.ClassCastException: java.util.HashMap


Anything I am missing.
 
vina parmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how do i do this.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I am not sure of why this is happening but it seems that if you cast your object to a HashMap instead of HashTable and then retrieve the value using a key, the code actually works!

 
vina parmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Monu,

I have tried HashMap and it works fine.
But in next Activity I have to use Vector then it will again create problem.

If you can find the solution please reply.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reminder: just because a container class is serializable doesn't mean that the container object is serializable. If you store a non-serializable object in a HashMap or Vector, the entire HashMap or Vector becomes effectively non-serializable.

This won't result in compile-time errors. The compiler cannot usually deduce such things.
 
vina parmar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim

It will help me a lot.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Reminder: just because a container class is serializable doesn't mean that the container object is serializable. If you store a non-serializable object in a HashMap or Vector, the entire HashMap or Vector becomes effectively non-serializable.

This won't result in compile-time errors. The compiler cannot usually deduce such things.


Good Point.

The strange thing for me however is the change in type information. I created a Hashtable(IS-A Serializable) and inserted just one String(IS-A Serializable) object into it. I passed this Hashtable as an intent extra. On the receiving end, I did a getSerializableExtra() to get Serializable which I was casting to Hashtable. JVM says the cast is Illegal. However, if I cast the object to a HashMap, I can read the values stored in the data structure.

I am wondering how a Hashtable type is converted to a HashMap type??!!
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hashtable is a subclass of java.util.dictionary

as of java1.2 hashtable implements map interface......

I think that may be the reason that hashmap accepts a hashtable object

maybe while reading it again it may have found only the map properties.....

I am given reasons vaguely.....but it may be the starting point of understanding the casting.

 
reply
    Bookmark Topic Watch Topic
  • New Topic