Android Parcelable Object在解组时给出ClassNotFoundException

时间:2014-05-17 21:30:33

标签: android android-intent classnotfoundexception parcelable badparcelableexception

我写了两个Parcelable类,即ScheduleItem和Venue。

ScheduleItem.java

public class ScheduleItem  implements Parcelable {
    private static final SimpleDateFormat FORMATTER = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss Z");

    @SerializedName("start_date")
    private Date mStartDate;

    @SerializedName("end_date")
    private Date mEndDate;


        .............................
        getter & setter
        ............................


    private ScheduleItem(Parcel in) {

        long[] lngData = new long[2];

        in.readLongArray(lngData);
        this.mStartDate = new Date(lngData[0]);
        this.mEndDate = new Date(lngData[1]);
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flag) {
        // TODO Auto-generated method stub
        out.writeLongArray(new long[]{ this.mStartDate.getTime(), this.mEndDate.getTime()});
    }

    public static final Parcelable.Creator<ScheduleItem> CREATOR = new Parcelable.Creator<ScheduleItem>() {
        public ScheduleItem createFromParcel(Parcel in) {
            return new ScheduleItem(in);
        }

        public ScheduleItem[] newArray(int size) {
            return new ScheduleItem[size];
        }
    };

}

Venue.java

public class Venue implements Parcelable {

    // Core fields
    @SerializedName("id")
    private long mId;

    @SerializedName("pcode")
    private int mPcode;

    @SerializedName("longitude")
    private double mLatitude;

    @SerializedName("latitude")
    private double mLongitude;

    @SerializedName("name")
    private String mName;

    @SerializedName("address")
    private String mAddress;

    @SerializedName("city")
    private String mCity;

    @SerializedName("state")
    private String mState;

    @SerializedName("zip")
    private String mZip;

    @SerializedName("phone")
    private String mPhone;

    // Super Bowl venue fields
    @SerializedName("tollfreephone")
    private String mTollFreePhone;

    private String mUrl;

    @SerializedName("description")
    private String mDescription;

    @SerializedName("ticket_link")
    private String mTicketLink;

    @SerializedName("image_url")
    private String mImageUrl;

    @SerializedName("schedule")
    private ArrayList<ScheduleItem> mSchedule = new ArrayList<ScheduleItem>();

    // computed fields
    private float mDistance;

    .............................
    getter & setter
    ............................

    private Venue(Parcel in) {

        this.mId = in.readLong();

        this.mPcode = in.readInt();

        double[] dbleData = new double[2];

        in.readDoubleArray(dbleData);
        this.mLatitude = dbleData[0];
        this.mLongitude = dbleData[1];

        String[] strData = new String[11];

        in.readStringArray(strData);
        this.mName = strData[0];
        this.mAddress = strData[1];
        this.mCity = strData[2];
        this.mState = strData[3];
        this.mZip = strData[4];
        this.mPhone = strData[5];
        this.mTollFreePhone = strData[6];
        this.mUrl = strData[7];
        this.mDescription = strData[8];
        this.mTicketLink = strData[9];
        this.mImageUrl = strData[10];

        this.mDistance = in.readFloat();

        List<ScheduleItem> tempSchedule = new ArrayList<ScheduleItem>();
        in.readList(tempSchedule, null);
        this.mSchedule = new ArrayList<ScheduleItem>(tempSchedule);

    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        // TODO Auto-generated method stub

        out.writeLong(this.mId);

        out.writeInt(this.mPcode);

        out.writeDoubleArray(new double[]{ this.mLatitude, this.mLongitude});

        out.writeStringArray(new String[] { this.mName, this.mAddress,
                this.mCity, this.mState, this.mZip, this.mPhone,
                this.mTollFreePhone, this.mUrl, this.mDescription,
                this.mTicketLink, this.mImageUrl });

        out.writeFloat(this.mDistance);

        out.writeList((List) this.mSchedule);
    }

    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 String toString(){
        return getName();
    }

}

现在我想通过意图将它从一个片段传递给下一个Activity。

Intent intent = new Intent();
intent.setClass(getActivity(), VenueDetailsActivity.class);
intent.putExtra("venueDetails", selectedVenue); //selectedVenue is a object of Venue
startActivity(intent);

在新的活动中,

Venue mSelectedVenue =(Venue)getIntent()。getParcelableExtra(                     “venueDetails”);

当ArrayList mSchedule大小为零时,它工作正常。但除此之外它正在抛出android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.ks.model.ScheduleItem

我的代码有什么问题?

0 个答案:

没有答案