Start a new topic

GenericJson as Serializable or Parcelable

Hello,



I'm using an object retrieved from Kinvey :

public class Car extends GenericJson implements Serializable {... }



I'm trying to move a Car object from one Activity to another :

In ActivityA :

Bundle bundle = new Bundle();

bundle.putSerializable("car", car);

Intent intent = new Intent(context, ActivityB.class);

intent.putExtras(bundle);

startActivity(intent);



In ActivityB :

Bundle bundle = this.getIntent().getExtras();

car = (Car) bundle.get("car");



But I'm facing an exception : java.lang.ClassCastException: java.util.HashMap cannot be cast to com.mypackage.Car



Could you help me to make it working ?



Thank you.

Sukhpal,


We do not generally provide custom code for people.  There are perfectly working examples of code above in this thread outlining not only how to use it, but with Ed going into detail of expected data types, returns, etc.  This entire example is rather contingent upon the last line that Ed said, which is: 


 

Car car = new Car((HashMap) this.getIntent().getSerializableExtra("car"));

 

Without that, you will get a type error.  If you are having issues that defy the code provided above (which you have not proven at this time) please let us know.


Thanks,

hi.. anybody can provide code .. i have same problem in serialization of GenericJson extended class..


public Child(HashMap map){

 

  for(String key : map.keySet()){

   this.put(key, map.get(key));

  }

 }


i put this constructor in class but is show error : Type mismatch cannot convert from element type object to string.


thnx.

Hello Edward,



Thank you so much , it works with Serializable !



With Parcelable, my log level was "error" but "java.lang.ClassCastException: java.util.HashMap cannot be cast to android.os.Parcelable" appears only in "debug" level ! I have just discovered it.



Using Parcelable instead of Serializable is the official best way. Perhaps you could replace GenericJson by a Kinvey class (not extending a Map) ?
Hey,



When I tried this with Parcelable I got the error:



`Key car expected Parcelable but value was a java.util.HashMap. The default value was returned.`



When I tried this with Serializable, I got the error:



`java.lang.ClassCastException: java.util.HashMap cannot be cast to android.os.Bundle`



It looks like because GenericJson extends an ArrayMap, Android is attempting to treat it like a HashMap when it is packaged up.



As a workaround, you can add a new constructor to` Car` that takes a Hashmap, and use this from the second activity. I've only tested this with `Serializable`, but it does work





In `Car`:



public Car(HashMap map){

for (String key : map.keySet()){

this.put(key, map.get(key));

}

}





In `ActivityB`:



Car car = new Car((HashMap) this.getIntent().getSerializableExtra("car"));



Hello Edward,



I have tried both but I'm trying with Parcelable now.



This is an example of a Parcelable without error :



Car :

public class Car implements Parcelable {

private Integer id;

private Integer name;



public Car(){}



public Integer getId() {

return id;

}



public void setId(Integer id) {

this.id = id;

}



public Integer getName() {

return name;

}



public void setName(Integer name) {

this.name = name;

}



@Override

public int describeContents() {

return 0;

}



@Override

public void writeToParcel(Parcel dest, int flags) {

dest.writeValue(this.id);

dest.writeValue(this.name);

}



private Car(Parcel in) {

this.id = (Integer) in.readValue(Integer.class.getClassLoader());

this.name = (Integer) in.readValue(Integer.class.getClassLoader());

}



public static final Creator
public Car createFromParcel(Parcel source) {

return new Car(source);

}



public Car[] newArray(int size) {

return new Car[size];

}

};

}



In ActivityA :

> Intent intent = new Intent(context, ActivityB.class);

> intent.putExtra("car", new Car());

> startActivity(intent);



In ActivityB :

> Car car = (Car) this.getIntent().getParcelableExtra("car");

> car.getName(); // car is not null, perfect !



If I only add "extends GenericJson" to Car, this.getIntent().getParcelableExtra("car") is null !
Hey,



are you using Parcelable or Serializable?



If you are still using:



public class Car extends GenericJson implements Serializable {... }



then in ActivityB, it should be:



car = (Car)getIntent().getSerializableExtra("car");





Also, you will need all the jars we distribute within your project -- so good catch on the Class not Found. Here is a post with more information and how to use the two approachs (Serializible vs Parcelable)



http://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html
Hello Damien, thank you for the answer.



I'm trying again and I have no problem with another Serializable class. I'm trying with a Parcelable and I found "Could not find class 'com.google.api.client.json.jackson2.JacksonFactory', referenced from method com.kinvey.android.AndroidJson.newCompatibleJsonFactory"



The error does not appear in the logs if I add compile files('libs/kinvey/google-http-client-jackson-1.19.0.jar')



But I still have a NullPointerException when sharing my Parcelable between 2 Activities :



In ActivityA :

> Intent intent = new Intent(context, ActivityB.class);

> intent.putExtra("car", track);

> startActivity(intent);



In ActivityB :

> car = (Car) this.getIntent().getParcelableExtra("car");

> // car is null here
Fabreax: Have you tried creating a hashmap with as the types? As that seems to be the issue that you're facing here. Bundle was created as a hashmap, so when you invoke bundle.get("car") you are getting a hashmap in return.



Some example code where this is implemented properly would look something like this:



Bundle bundle = this.getIntent().getExtras();

if(bundle!=null){

Hash_Map= bundle.getSerializable("car");

}
I will try with Parcelable
Login or Signup to post a comment