我试图将一个对象从一个活动传递到另一个活动。我按照本教程enter link description here进行了操作,但我得到了例外:
引起:java.lang.RuntimeException:Parcel android.os.Parcel@41ed4e70: 在偏移332处解组未知类型代码6357091
我的课程是:
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;
}
}
我尝试传递数组时的代码:
Intent intent = new Intent(getActivity().getApplicationContext(), MoreDatesAndPlacesActivity.class);
intent.putParcelableArrayListExtra("venues", (ArrayList<? extends Parcelable>) currentEventModel.getVenues());
当我尝试获取数组时的代码:
Bundle b = getIntent().getExtras();
List<Venue> venues = b.getParcelableArrayList("venues");
我该如何解决这个例外?感谢
答案 0 :(得分:2)
我猜你的endDate == null
对象Occurrence
时会发生这种情况。在这种情况下,您没有为Parcel写入值,但readFromParcel()
方法仍在尝试读取它。
也许以下内容会有所帮助:
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);
}
}
答案 1 :(得分:1)
您可以使用android studio插件将make类作为parcelable https://plugins.jetbrains.com/plugin/7332?pr=