Parcelable从阵列到OOM获得巨大的无限项目

时间:2017-06-06 11:39:32

标签: android parcelable

我正在使用Parcelable在片段之间进行通信。一切都运行良好,但有时当Android杀死应用程序进程以释放内存并且用户返回应用程序时,Parcelable从数组中获取大量项目,例如数百万项而不是2-3项,当然它会抛出OOM。 我想CREATOR有些不对劲,但是我无法处理它。 parcelable的代码由超类和子类以及我感兴趣的项类组成:

基类:

 public abstract class BaseRs implements Parcelable {

private String token;
private String msgAlert;
private String simpleMsg;
private AdsObj adsObj;    
private Map<String, String> mapSettings;

//getters & setters

public BaseRs() {
}


protected BaseRs(Parcel in) {
    token = in.readString();
    msgAlert = in.readString();
    simpleMsg = in.readString();
    adsObj = in.readParcelable(AdsObj.class.getClassLoader());
    mapSettings = MapParcelable.readParcelable(in);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(token);
    dest.writeString(msgAlert);
    dest.writeString(simpleMsg);
    dest.writeParcelable(adsObj, flags);
    MapParcelable.writeToParcel(dest, mapSettings);
 }

}

儿童班

public class GetSalesItemsRs extends BaseRs {

private SaleCoinItem[] coinPacksArray;

//getters+ setters


protected GetSalesItemsRs(Parcel in) {
    super(in);
    coinPacksArray = in.createTypedArray(SaleCoinItem.CREATOR);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    super.writeToParcel(dest, flags);
    dest.writeTypedArray(coinPacksArray, flags);
}

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

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

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

和我感兴趣的对象

public class SaleCoinItem implements Parcelable {

private int amount;
private double price;
private int sortOrder;

//getters & setters

protected SaleCoinItem(Parcel in) {
    amount = in.readInt();
    price = in.readDouble();
    sortOrder = in.readInt();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(amount);
    dest.writeDouble(price);
    dest.writeInt(sortOrder);
}

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

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

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


       }
    };
}

包裹地图的功能 - 可能在这里是错误;)

public class MapParcelable {

public static void writeToParcel(Parcel out, Map<String, String> map) {
    if (map != null) {
        out.writeInt(map.size());
        for (Map.Entry<String, String> entry : map.entrySet()) {
            out.writeString(entry.getKey());
            out.writeString(entry.getValue());
        }
    }
}

public static Map<String, String> readParcelable(Parcel parcel) {
    Map<String, String> map = new HashMap<String, String>();
    int size = parcel.readInt();
    for (int i = 0; i < size; i++) {
        String key = parcel.readString();
        String value = parcel.readString();
        map.put(key, value);
    }

    return map;


   }

}

我在onCreateView方法中获得了parcelable obj:

GetSalesItemsRs mGetSalesItemsRs = getArguments().getParcelable(KEY_PARCEL);

1 个答案:

答案 0 :(得分:1)

我认为问题在于,如果Map为null,则不会将大小字段写入Parcel,但是当您读取它时,您总是会读取size字段,并且最终可能会读取一些随机值。所以或者你将代码更改为

if (map != null) {
    out.writeInt(map.size());
    for (Map.Entry<String, String> entry : map.entrySet()) {
        out.writeString(entry.getKey());
        out.writeString(entry.getValue());
    }
}else{
    out.writeInt(0);
}

并且在重新创建类时永远不会获得空值,或者您创建另一个布尔字段&#34; hasMap&#34;并且总是写一个