Reputation: 397
I'm trying to pass an object from one activity to another. I followed this tutorial enter link description here but I'm getting the exception:
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@41ed4e70: Unmarshalling unknown type code 6357091 at offset 332
My classes are:
public class Venue implements Parcelable{
private int id;
private String name;
@SerializedName("address")
private String location;
@SerializedName("lat")
private double latitude;
@SerializedName("lon")
private double longitude;
@SerializedName("postal_code")
private String postalCode;
private String country;
private String phone;
private String website;
@SerializedName("foursquare_id")
private String foursquareId;
@SerializedName("location_id")
private int locationId;
private String city;
@SerializedName("Occurrence")
private Occurrence occurrence;
public Venue(){}
public Venue(int id, String name, String location){
this.id = id;
this.name = name;
this.location = location;
}
public Venue(Parcel in){
readFromParcel(in);
}
/**
* Clase para recuperar los datos de un parcel, IMPORTANTE leerlos en el mismo orden que se escribieron!
* @param in Parcel con los datos a leer
*/
private void readFromParcel(Parcel in) {
this.id = in.readInt();
this.name = in.readString();
this.location = in.readString();
this.latitude = in.readDouble();
this.longitude = in.readDouble();
this.occurrence = in.readParcelable(Occurrence.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.name);
dest.writeString(this.location);
dest.writeDouble(this.latitude);
dest.writeDouble(this.longitude);
dest.writeParcelable(this.occurrence, flags);
}
public static final Parcelable.Creator<Venue> CREATOR = new Parcelable.Creator<Venue>() {
public Venue createFromParcel(Parcel in) {
return new Venue(in);
}
public Venue[] newArray(int size) {
return new Venue[size];
}
};
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public Occurrence getOccurrence() {
return occurrence;
}
public void setOccurrence(Occurrence occurrence) {
this.occurrence = occurrence;
}
@Override
public int describeContents() {
return 0;
}
}
public class Occurrence implements Parcelable{
private int id;
@SerializedName("start_date")
private Date startDate;
@SerializedName("end_date")
private Date endDate;
private Event event;
public Occurrence(Parcel in){
readFromParcel(in);
}
/**
* Clase para recuperar los datos de un parcel, IMPORTANTE leerlos en el mismo orden que se escribieron!
* @param in Parcel con los datos a leer
*/
private void readFromParcel(Parcel in) {
id = in.readInt();
startDate = new Date(in.readLong());
endDate = new Date(in.readLong());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeLong(startDate.getTime());
if (endDate != null)
dest.writeLong(endDate.getTime());
}
public static final Parcelable.Creator<Occurrence> CREATOR = new Parcelable.Creator<Occurrence>() {
public Occurrence createFromParcel(Parcel in) {
return new Occurrence(in);
}
public Occurrence[] newArray(int size) {
return new Occurrence[size];
}
};
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public Event getEvent() {
return event;
}
public void setEvent(Event event) {
this.event = event;
}
@Override
public int describeContents() {
return 0;
}
}
And the code when I try to pass the Array:
Intent intent = new Intent(getActivity().getApplicationContext(), MoreDatesAndPlacesActivity.class);
intent.putParcelableArrayListExtra("venues", (ArrayList<? extends Parcelable>) currentEventModel.getVenues());
And the code when I try to get the array:
Bundle b = getIntent().getExtras();
List<Venue> venues = b.getParcelableArrayList("venues");
How can i solve this exception? Thanks
Upvotes: 2
Views: 5682
Reputation: 86
You can use android studio plug in for make class as parcelable https://plugins.jetbrains.com/plugin/7332?pr=
Upvotes: 1
Reputation: 39191
I would guess that this is happening when endDate == null
in your Occurrence
object. In this case, you're not writing a value to the Parcel, but the readFromParcel()
method is still trying to read it.
Maybe something like the following will help:
private void readFromParcel(Parcel in)
{
id = in.readInt();
startDate = new Date(in.readLong());
long date = in.readLong();
if (date != 0L)
{
endDate = new Date(date);
}
}
public void writeToParcel(Parcel dest, int flags)
{
dest.writeInt(id);
dest.writeLong(startDate.getTime());
if (endDate != null)
{
dest.writeLong(endDate.getTime());
}
else
{
dest.writeLong(0L);
}
}
Upvotes: 2