将包含parcelable对象的hashmap写入parcel时,应用程序崩溃

时间:2017-04-29 05:02:27

标签: android parcelable

这是我的DataModel:

public class SellerCategoriesData implements Parcelable {
private LinkedHashMap<String, ArrayList<CategoryData>> categoriesDataMap;

public LinkedHashMap<String, ArrayList<CategoryData>> getCategoriesDataMap() {
    return categoriesDataMap;
}

public void setCategoriesDataMap(LinkedHashMap<String, ArrayList<CategoryData>> categoriesDataMap) {
    this.categoriesDataMap = categoriesDataMap;
}

public SellerCategoriesData() {
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeSerializable(this.categoriesDataMap);
}

protected SellerCategoriesData(Parcel in) {
    this.categoriesDataMap = (LinkedHashMap<String, ArrayList<CategoryData>>) in.readSerializable();
}

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

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

这是CategoriesData Model:

public class CategoryData implements Parcelable
{
    private String id;
    private String name;
    private String bucket;
    private String type;
    private String thumbnail;
    private String context;
    private String category_id;
    private String icon_url;

    public CategoryData()
    {

    }

    public static CategoryData getCategoryData(String name)
    {
        CategoryData categoryData = new CategoryData();
        categoryData.setName(name);

        return categoryData;
    }

    protected CategoryData(Parcel in)
    {
        id = in.readString();
        name = in.readString();
        bucket = in.readString();
        type = in.readString();
        thumbnail = in.readString();
        context = in.readString();
        category_id = in.readString();
    }

    public static final Creator<CategoryData> CREATOR = new Creator<CategoryData>()
    {
        @Override
        public CategoryData createFromParcel(Parcel in)
        {
            return new CategoryData(in);
        }

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

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

    @Override
    public void writeToParcel(Parcel parcel, int i)
    {
        parcel.writeString(id);
        parcel.writeString(name);
        parcel.writeString(bucket);
        parcel.writeString(type);
        parcel.writeString(thumbnail);
        parcel.writeString(context);
        parcel.writeString(category_id);
    }

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getBucket()
    {
        return bucket;
    }

    public void setBucket(String bucket)
    {
        this.bucket = bucket;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public String getThumbnail()
    {
        return thumbnail;
    }

    public void setThumbnail(String thumbnail)
    {
        this.thumbnail = thumbnail;
    }

    public String getContext()
    {
        return context;
    }

    public void setContext(String context)
    {
        this.context = context;
    }

    public String getCategory_id() {
        return category_id;
    }

    public void setCategory_id(String category_id) {
        this.category_id = category_id;
    }

    public String getIcon_url() {
        return icon_url;
    }

    public void setIcon_url(String icon_url) {
        this.icon_url = icon_url;
    }
}

但是app有时会出现以下异常:

Fatal Exception: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = java.util.LinkedHashMap)
   at android.os.Parcel.writeSerializable(Parcel.java:1468)
   at in.droom.v2.model.SellerCategoriesData.writeToParcel(SellerCategoriesData.java:59)
   at android.os.Parcel.writeParcelable(Parcel.java:1437)
   at in.droom.v2.model.selleronboarding.UserAccountSettingsModel.writeToParcel(UserAccountSettingsModel.java:112)
   at android.os.Parcel.writeParcelable(Parcel.java:1437)
   at android.os.Parcel.writeValue(Parcel.java:1343)
   at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
   at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
   at android.os.Bundle.writeToParcel(Bundle.java:1079)
   at android.os.Parcel.writeBundle(Parcel.java:711)
   at android.app.FragmentState.writeToParcel(Fragment.java:140)
   at android.os.Parcel.writeTypedArray(Parcel.java:1254)
   at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:380)
   at android.os.Parcel.writeParcelable(Parcel.java:1437)
   at android.os.Parcel.writeValue(Parcel.java:1343)
   at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
   at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
   at android.os.Bundle.writeToParcel(Bundle.java:1079)
   at android.os.Parcel.writeBundle(Parcel.java:711)
   at android.app.FragmentState.writeToParcel(Fragment.java:141)
   at android.os.Parcel.writeTypedArray(Parcel.java:1254)
   at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:380)
   at android.os.Parcel.writeParcelable(Parcel.java:1437)
   at android.os.Parcel.writeValue(Parcel.java:1343)
   at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
   at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
   at android.os.Bundle.writeToParcel(Bundle.java:1079)
   at android.os.Parcel.writeBundle(Parcel.java:711)
   at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3295)
   at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3431)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5437)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by java.io.NotSerializableException: in.droom.v2.model.CategoryData
   at 
java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
   at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
   at java.util.ArrayList.writeObject(ArrayList.java:648)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1033)
   at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
   at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
   at java.util.HashMap.writeObject(HashMap.java:976)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1033)
   at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
   at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
   at android.os.Parcel.writeSerializable(Parcel.java:1463)
   at in.droom.v2.model.SellerCategoriesData.writeToParcel(SellerCategoriesData.java:59)
   at android.os.Parcel.writeParcelable(Parcel.java:1437)
   at in.droom.v2.model.selleronboarding.UserAccountSettingsModel.writeToParcel(UserAccountSettingsModel.java:112)
   at android.os.Parcel.writeParcelable(Parcel.java:1437)
   at android.os.Parcel.writeValue(Parcel.java:1343)
   at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
   at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
   at android.os.Bundle.writeToParcel(Bundle.java:1079)
   at android.os.Parcel.writeBundle(Parcel.java:711)
   at android.app.FragmentState.writeToParcel(Fragment.java:140)
   at android.os.Parcel.writeTypedArray(Parcel.java:1254)
   at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:380)
   at android.os.Parcel.writeParcelable(Parcel.java:1437)
   at android.os.Parcel.writeValue(Parcel.java:1343)
   at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
   at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
   at android.os.Bundle.writeToParcel(Bundle.java:1079)
   at android.os.Parcel.writeBundle(Parcel.java:711)
   at android.app.FragmentState.writeToParcel(Fragment.java:141)
   at android.os.Parcel.writeTypedArray(Parcel.java:1254)
   at android.app.FragmentManagerState.writeToParcel(FragmentManager.java:380)
   at android.os.Parcel.writeParcelable(Parcel.java:1437)
   at android.os.Parcel.writeValue(Parcel.java:1343)
   at android.os.Parcel.writeArrayMapInternal(Parcel.java:686)
   at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
   at android.os.Bundle.writeToParcel(Bundle.java:1079)
   at android.os.Parcel.writeBundle(Parcel.java:711)
   at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3295)
   at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3431)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5437)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是行号。 59:

    dest.writeSerializable(this.categoriesDataMap);

它是一个包含parcelable对象的hashmap。所以,我的问题是我们可以将包含parcelable对象的hashmap写入parcel吗?

0 个答案:

没有答案