在parcelable对象

时间:2015-08-08 13:06:01

标签: android fragment parcelable parcel

解组一个可分割的对象时遇到问题。 该例外每次都不可重复,但有时是。 以下是可分配对象的定义:

public class Article extends DBOperationHolder implements Parcelable {

private static final String TAG = "Article_Object";

@SerializedName("article_id")
private int mId;

@SerializedName("city_id")
private int mCityId;

@SerializedName("categories")
private List<Integer> mCategories;

@SerializedName("neighborhood_tags")
private List<Integer> mNeighborhoodTags;

@SerializedName("cuisine_tags")
private List<Integer> mCuisineTags;

@SerializedName("collection_articles")
private List<Integer> mCollectionArticles;

@SerializedName("rating")
private int mRating;

@SerializedName("title")
private String mTitle;

@SerializedName("subtitle")
private String mSubTitle;

@SerializedName("summary")
private String mSummary;

@SerializedName("text")
private String mText;

@SerializedName("main_image_url")
private String mMainImgUrl;

@SerializedName("images")
private List<String> mImages;

@SerializedName("date_from")
private String mDateFrom;

@SerializedName("date_to")
private String mDateTo;

@SerializedName("venue_name")
private String mVenueName;

@SerializedName("address")
private String mAddress;

@SerializedName("address_latitude")
private double mAddressLatitude;

@SerializedName("address_longitude")
private double mAddressLongitude;

@SerializedName("contact_person")
private String mContactPerson;

@SerializedName("contact_email")
private String mContactEmail;

@SerializedName("contact_telephone")
private String mContactTelephone;

@SerializedName("contact_website")
private String mContactWebsite;

@SerializedName("style")
private String mStyle;

@SerializedName("ideal_for")
private String mIdealFor;

@SerializedName("favorite_tables")
private String mFavoriteTables;

@SerializedName("favorite_room")
private String mFavoriteRoom;

@SerializedName("signature_dishes")
private String mSignatureDishes;

@SerializedName("signature_drinks")
private String mSignatureDrinks;

@SerializedName("highlights")
private String mHighLights;

@SerializedName("type")
private String mType;

@SerializedName("membership_price")
private String mMemberShipPrice;

@SerializedName("membership_benefits")
private String mMemberShipBenefits;

@SerializedName("publish_date")
private String mPublishDate;

@SerializedName("article_url")
private String mArticleUrl;

private String mTag;
private boolean mIsCollection;
private int mIsAddedInFavorites;
private int mCategoryColor;
private String mCuisine;
private int mCategoryId;

public Article() {

}

public Article(int id) {
    mId = id;
}

private Article(Parcel in) {
    mId = in.readInt();
    mCityId = in.readInt();
    mRating = in.readInt();
    mTitle = in.readString();
    mSubTitle = in.readString();
    mSummary = in.readString();
    mText = in.readString();
    mMainImgUrl = in.readString();
    mDateFrom = in.readString();
    mDateTo = in.readString();
    mVenueName = in.readString();
    mAddress = in.readString();
    mAddressLatitude = in.readFloat();
    mAddressLongitude = in.readFloat();
    mContactPerson = in.readString();
    mContactEmail = in.readString();
    mContactTelephone = in.readString();
    mContactWebsite = in.readString();
    mStyle = in.readString();
    mIdealFor = in.readString();
    mFavoriteTables = in.readString();
    mFavoriteRoom = in.readString();
    mSignatureDishes = in.readString();
    mSignatureDrinks = in.readString();
    mHighLights = in.readString();
    mType = in.readString();
    mMemberShipPrice = in.readString();
    mMemberShipBenefits = in.readString();
    mPublishDate = in.readString();
    mIsAddedInFavorites = in.readInt();
    mTag = in.readString();
    mCategoryColor = in.readInt();
    mCuisine = in.readString();
    mCategoryId = in.readInt();
    mArticleUrl = in.readString();
}

@Override
public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeInt(mId);
    parcel.writeInt(mCityId);
    parcel.writeInt(mRating);
    parcel.writeString(mTitle);
    parcel.writeString(mSubTitle);
    parcel.writeString(mSummary);
    parcel.writeString(mText);
    parcel.writeString(mMainImgUrl);
    parcel.writeString(mDateFrom);
    parcel.writeString(mDateTo);
    parcel.writeString(mVenueName);
    parcel.writeString(mAddress);
    parcel.writeDouble(mAddressLatitude);
    parcel.writeDouble(mAddressLongitude);
    parcel.writeString(mContactPerson);
    parcel.writeString(mContactEmail);
    parcel.writeString(mContactTelephone);
    parcel.writeString(mContactWebsite);
    parcel.writeString(mStyle);
    parcel.writeString(mIdealFor);
    parcel.writeString(mFavoriteTables);
    parcel.writeString(mFavoriteRoom);
    parcel.writeString(mSignatureDishes);
    parcel.writeString(mSignatureDrinks);
    parcel.writeString(mHighLights);
    parcel.writeString(mType);
    parcel.writeString(mMemberShipPrice);
    parcel.writeString(mMemberShipBenefits);
    parcel.writeString(mPublishDate);
    parcel.writeInt(mIsAddedInFavorites);
    parcel.writeString(mTag);
    parcel.writeInt(mCategoryColor);
    parcel.writeString(mCuisine);
    parcel.writeInt(mCategoryId);
    parcel.writeString(mArticleUrl);
}


@Override
public int describeContents() {
    return 0;
}

public static Creator<Article> CREATOR = new Creator<Article>() {
    @Override
    public Article createFromParcel(Parcel source) {
        return new Article(source);
    }

    @Override
    public Article[] newArray(int size) {
        return new Article[size];
    }
};


public String getMainImgUrl() {
    if (TextUtils.isEmpty(mMainImgUrl)) {
        if (!CollectionUtil.isEmpty(mImages)) {
            mMainImgUrl = mImages.get(0);
        }
    }
    return mMainImgUrl;
}

public int getId() {
    return mId;
}

public void setId(int id) {
    mId = id;
}

public int getCityId() {
    return mCityId;
}

public void setCityId(int cityId) {
    mCityId = cityId;
}

public List<Integer> getCategories() {
    return mCategories;
}

public void setCategories(List<Integer> categories) {
    mCategories = categories;
}

public List<Integer> getNeighborhoodTags() {
    return mNeighborhoodTags;
}

public void setNeighborhoodTags(List<Integer> neighborhoodTags) {
    mNeighborhoodTags = neighborhoodTags;
}

public List<Integer> getCuisineTags() {
    return mCuisineTags;
}

public void setCuisineTags(List<Integer> cuisineTags) {
    mCuisineTags = cuisineTags;
}

public List<Integer> getCollectionArticles() {
    return mCollectionArticles;
}

public void setCollectionArticles(List<Integer> collectionArticles) {
    mCollectionArticles = collectionArticles;
}

public int getRating() {
    return mRating;
}

public void setRating(int rating) {
    mRating = rating;
}

public String getTitle() {
    return mTitle;
}

public void setTitle(String title) {
    mTitle = title;
}

public String getSubTitle() {
    return mSubTitle;
}

public void setSubTitle(String subTitle) {
    mSubTitle = subTitle;
}

public String getSummary() {
    return mSummary;
}

public void setSummary(String summary) {
    mSummary = summary;
}

public String getText() {
    return mText;
}

public void setText(String text) {
    mText = text;
}

public void setMainImgUrl(String mainImgUrl) {
    mMainImgUrl = mainImgUrl;
}

public List<String> getImages() {
    return mImages;
}

public void setImages(List<String> images) {
    mImages = images;
}

public String getDateFrom() {
    return mDateFrom;
}

public void setDateFrom(String dateFrom) {
    mDateFrom = dateFrom;
}

public String getDateTo() {
    return mDateTo;
}

public void setDateTo(String dateTo) {
    mDateTo = dateTo;
}

public String getVenueName() {
    return mVenueName;
}

public void setVenueName(String venueName) {
    mVenueName = venueName;
}

public String getAddress() {
    return mAddress;
}

public void setAddress(String address) {
    mAddress = address;
}

public double getLatitude() {
    return mAddressLatitude;
}

public void setLatitude(double addressLatitude) {
    mAddressLatitude = addressLatitude;
}

public double getLongitude() {
    return mAddressLongitude;
}

public void setLongitude(double addressLongitude) {
    mAddressLongitude = addressLongitude;
}

public String getContactPerson() {
    return mContactPerson;
}

public void setContactPerson(String contactPerson) {
    mContactPerson = contactPerson;
}

public String getContactEmail() {
    return mContactEmail;
}

public void setContactEmail(String contactEmail) {
    mContactEmail = contactEmail;
}

public String getContactTelephone() {
    return mContactTelephone;
}

public void setContactTelephone(String contactTelephone) {
    mContactTelephone = contactTelephone;
}

public String getContactWebsite() {
    return mContactWebsite;
}

public void setContactWebsite(String contactWebsite) {
    mContactWebsite = contactWebsite;
}

public String getStyle() {
    return mStyle;
}

public void setStyle(String style) {
    mStyle = style;
}

public String getIdealFor() {
    return mIdealFor;
}

public void setIdealFor(String idealFor) {
    mIdealFor = idealFor;
}

public String getFavoriteTables() {
    return mFavoriteTables;
}

public void setFavoriteTables(String favoriteTables) {
    mFavoriteTables = favoriteTables;
}

public String getFavoriteRoom() {
    return mFavoriteRoom;
}

public void setFavoriteRoom(String favoriteRoom) {
    mFavoriteRoom = favoriteRoom;
}

public String getSignatureDishes() {
    return mSignatureDishes;
}

public void setSignatureDishes(String signatureDishes) {
    mSignatureDishes = signatureDishes;
}

public String getSignatureDrinks() {
    return mSignatureDrinks;
}

public void setSignatureDrinks(String signatureDrinks) {
    mSignatureDrinks = signatureDrinks;
}

public String getHighLights() {
    return mHighLights;
}

public void setHighLights(String highLights) {
    mHighLights = highLights;
}

public String getType() {
    return mType;
}

public void setType(String type) {
    mType = type;
}

public String getMemberShipPrice() {
    return mMemberShipPrice;
}

public void setMemberShipPrice(String memberShipPrice) {
    mMemberShipPrice = memberShipPrice;
}

public String getMemberShipBenefits() {
    return mMemberShipBenefits;
}

public void setMemberShipBenefits(String memberShipBenefits) {
    mMemberShipBenefits = memberShipBenefits;
}

public String getPublishDate() {
    return mPublishDate;
}

public void setPublishDate(String publishDate) {
    mPublishDate = publishDate;
}

public String getArticleUrl() {
    return mArticleUrl;
}

public void setArticleUrl(String articleUrl) {
    mArticleUrl = articleUrl;
}

public String getTag() {
    return mTag;
}

public void setTag(String tag) {
    UrbLog.d(TAG,"setTag("+tag+")");
    mTag = tag;
}

public boolean isCollection() {
   return mCategoryId == Category.COLLECTION_ID;
}

public int getIsAddedInFavorites() {
    return mIsAddedInFavorites;
}

public void setIsAddedInFavorites(int isAddedInFavorites) {
    mIsAddedInFavorites = isAddedInFavorites;
}

public int getCategoryColor() {
    return mCategoryColor;
}

public void setCategoryColor(int categoryColor) {
    mCategoryColor = categoryColor;
}

public String getCuisine() {
    return mCuisine;
}

public void setCuisine(String cuisine) {
    mCuisine = cuisine;
}

public int getCategoryId() {
    return mCategoryId;
}

public void setCategoryId(int categoryId) {
    mCategoryId = categoryId;
}

}

我试图在片段的onCreate方法中解组parcelable

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle args = getArguments();
    if (args != null) {
        mArticle = args.getParcelable(Consts.ARTICLE_KEY);
    }
}

然后不是每次我都遇到以下状态的崩溃:

Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@3348215c: Unmarshalling unknown type code 7602280 at offset 4068
   at android.os.Parcel.readValue(Parcel.java:2222)
   at android.os.Parcel.readArrayMapInternal(Parcel.java:2479)
   at android.os.BaseBundle.unparcel(BaseBundle.java:221)
   at android.os.Bundle.getParcelable(Bundle.java:804)
   at com.appscollider.urbanologie.fragments.ArticleFragment.onCreate(ArticleFragment.java:233)
   at android.support.v4.app.Fragment.performCreate(Fragment.java:1766)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1120)
   at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1924)
   at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:268)
   at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:58)

该异常的原因是什么以及我如何解决它。 谢谢。

1 个答案:

答案 0 :(得分:0)

您的mAddressLatitudemAddressLongitude类型为double,并且您将它们解组为float类型:

mAddressLatitude = in.readFloat();
mAddressLongitude = in.readFloat();

如果你把它写成双重应该是:

mAddressLatitude = in.readDouble();
mAddressLongitude = in.readDouble();