readTypedList在Android中使用Parcelable时抛出OutOfMemoryError

时间:2018-05-03 19:27:49

标签: java android android-intent parcelable parcel

我有三个模型如下:

坐标模型

public class Coordinate implements Parcelable{
    private static final long serialVersionUID = 1L;
    private double latitude,longitude;
    private double x,y;
    private double bearing,distance;

    protected Coordinate(Parcel in) {
        latitude = in.readDouble();
        longitude = in.readDouble();
        x = in.readDouble();
        y = in.readDouble();
        bearing = in.readDouble();
        distance = in.readDouble();
    }

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

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeDouble(latitude);
        dest.writeDouble(longitude);
        dest.writeDouble(x);
        dest.writeDouble(y);
        dest.writeDouble(bearing);
        dest.writeDouble(distance);
    }



    public Coordinate(double lat,double lng,double x,double y,double distance,double bearing){
        this.latitude=lat;
        this.longitude=lng;
        this.x=x;
        this.y=y;
        this.distance=distance;
        this.bearing=bearing;

    }

    private Coordinate() {
        // TODO Auto-generated constructor stub
    }

    public double getBearing() {
        return bearing;
    }
    public double getDistance() {
        return distance;
    }

    public double getLatitude() {
        return latitude;
    }
    public double getX() {
        return x;
    }
    public double getY() {
        return y;
    }
    public double getLongitude() {
        return longitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void setBearing(double bearing) {
        this.bearing = bearing;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }
}

使用坐标的区域模型

public class Area implements Parcelable{
    private static final long serialVersionUID = 1L;
    private List<Coordinate> coordinates;
    private static Double R=63710000.0;

    protected Area(Parcel in) {
        coordinates = new ArrayList<Coordinate>();
        in.readTypedList(coordinates,Coordinate.CREATOR);
    }

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

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(coordinates);
    }



    private Area() {

    }
    public Area(List<Coordinate> coordinates) {
        this.coordinates = coordinates;
    }
    public Area(LatLng origin){
        coordinates=new ArrayList<Coordinate>();
        coordinates.add(new Coordinate(origin.latitude,origin.longitude,0,0,0,0));
    }

    public double getArea(){
        double ar=0,x1=0,y1=0,x2=0,y2=0;
        if(coordinates.size()>2) {
            for (int i = 1; i < coordinates.size(); i++) {
                x1 = coordinates.get(i).getX();
                y1 = coordinates.get(i).getY();
                x2 = coordinates.get(i - 1).getX();
                y2 = coordinates.get(i - 1).getY();
                ar += x1 * y2 - x2 * y1;

            }
            ar+=x1 *coordinates.get(0).getY()-y1*coordinates.get(0).getX();
            ar=Math.abs(ar)/2;
        }
        return ar;
    }

    public List<Coordinate> getCoordinates() {
        return coordinates;
    }

    public void addLatLng(LatLng latLng){
        double bearing =this.getBearing(latLng);
        double distance=this.getDistance(latLng);
        double y=distance*Math.cos(bearing);
        double x=distance*Math.sin(bearing);
        coordinates.add(new Coordinate(latLng.latitude,latLng.longitude,x,y,distance,bearing));
    }

    public boolean removeLatLan(){
        //returns true if Coordinate is removed and false if only last coordinate is left
        //does not remove the last coordinate
        if(coordinates.size()<=1){
            return false;
        }
        coordinates.remove(coordinates.size()-1);
        return true;

    }

    public List<LatLng> getLatLngList(){
        List<LatLng> res=new ArrayList<LatLng>(coordinates.size());
        for(Coordinate c:coordinates){
            LatLng l=new LatLng(c.getLatitude(),c.getLongitude());
            res.add(l);
        }
        return res;
    }
    private double getDistance(LatLng latLng){
        Coordinate origin=coordinates.get(0);
        double ph1=origin.getLatitude()*Math.PI/180;
        double ph2=latLng.latitude*Math.PI/180;
        double delTa=(latLng.longitude-origin.getLongitude())*Math.PI/180;
        double distance=R*Math.acos(Math.sin(ph1)*Math.sin(ph2)+
                Math.cos(ph1)*Math.cos(ph2)*Math.cos(delTa));
        return distance;
    }
    private double getBearing(LatLng latLng){
        Coordinate origin=coordinates.get(0);
        double ph1=origin.getLatitude()*Math.PI/180;
        double ph2=latLng.latitude*Math.PI/180;
        double delTa=(latLng.longitude-origin.getLongitude())*Math.PI/180;
        double y= Math.sin(delTa)*Math.cos(ph2);
        double x= Math.cos(ph1)*Math.sin(ph2)-Math.sin(ph1)*Math.cos(ph2)*Math.cos(delTa);
        return Math.atan2(y,x);
    }

    public void setCoordinates(List<Coordinate> coordinates) {
        this.coordinates = coordinates;
    }
}

然后来农场模型

public class Farm implements Parcelable{
    private static final long serialVersionUID = 1L;
    Long id;
    String webSafeKey;
    Float productivity;
    String address;
    ArrayList<Area> areas;

    public Farm(Context context, com.appspot.myapi.snapapi.model.Farm farm){
        this.id = farm.getId();
        webSafeKey = farm.getWebSafeKey();
        productivity = 0f;
        address = farm.getAddress()==null?context.getResources().getString
                (R.string.address)
                +": "+context.getResources().getString(R.string.NA):farm.getAddress();
        areas = new ArrayList<Area>();
        for(com.appspot.myapi.snapapi.model.Area areas: farm.getAreas()){
            List<LatLng> list = areas.getLatLngList();
            Area area = new Area(new com.google.android.gms.maps.model.LatLng(
                    list.get(0).getLatitude(),list.get(0).getLongitude()
            ));
            for(int i=1;i<list.size();i++){
                area.addLatLng(new com.google.android.gms.maps.model.LatLng(
                        list.get(i).getLatitude(),list.get(i).getLongitude()
                ));
            }
            this.areas.add(area);
        }
    }

    protected Farm(Parcel in) {
        if (in.readByte() == 0) {
            id = null;
        } else {
            id = in.readLong();
        }
        webSafeKey = in.readString();
        productivity = in.readFloat();
        address = in.readString();
        areas = new ArrayList<Area>();
        in.readTypedList(areas,Area.CREATOR);
//        areas = in.readArrayList(null);
    }

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

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

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

    public void writeToParcel(Parcel out, int flags) {
        out.writeLong(id);
        out.writeString(webSafeKey);
        out.writeFloat(productivity);
        out.writeString(address);
        out.writeTypedList(areas);
    }

    public Double getTotalArea(){
        double totalArea=0d;
        for(Area area:areas){
            totalArea +=area.getArea();
        }
        return totalArea;
    }

    public Long getId() {
        return id;
    }

    public String getWebSafeKey() {
        return webSafeKey;
    }

    public float getProductivity() {
        return productivity;
    }

    public String getAddress() {
        return address;
    }

    public ArrayList<Area> getAreas() {
        return areas;
    }

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

    public void setWebSafeKey(String webSafeKey) {
        this.webSafeKey = webSafeKey;
    }

    public void setProductivity(Float productivity) {
        this.productivity = productivity;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setAreas(ArrayList<Area> areas) {
        this.areas = areas;
    }
}

现在的问题是,当我将Farm类作为额外内容传递给Intent并尝试在第二个Activity中读取它时,in.readTypedList(areas, Area.CREATOR)给出了OutOfMemoryError。准确地说,这是完整的堆栈跟踪:

java.lang.OutOfMemoryError: Failed to allocate a 30536292 byte allocation with 16041952 free bytes and 15MB until OOM
                                                                   at java.util.ArrayList.add(ArrayList.java:118)
                                                                   at android.os.Parcel.readTypedList(Parcel.java:2043)
                                                                   at in.agrosnap.android.helper.Farm.<init>(Farm.java:60)
                                                                   at in.agrosnap.android.helper.Farm$1.createFromParcel(Farm.java:67)
                                                                   at in.agrosnap.android.helper.Farm$1.createFromParcel(Farm.java:64)
                                                                   at android.os.Parcel.readParcelable(Parcel.java:2367)
                                                                   at android.os.Parcel.readValue(Parcel.java:2264)
                                                                   at android.os.Parcel.readArrayMapInternal(Parcel.java:2614)
                                                                   at android.os.BaseBundle.unparcel(BaseBundle.java:221)
                                                                   at android.os.Bundle.getParcelable(Bundle.java:786)
                                                                   at android.content.Intent.getParcelableExtra(Intent.java:5387)
                                                                   at in.agrosnap.android.FarmDetail.onCreate(FarmDetail.java:79)
                                                                   at android.app.Activity.performCreate(Activity.java:6259)
                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

有人可以解释我在哪里出错了,编组和解组用户定义类的实现是错误的吗?

我使用以下引用来编写此代码:

1 个答案:

答案 0 :(得分:1)

Farm课程中,您有以下两种匹配方法:

protected Farm(Parcel in) {
    if (in.readByte() == 0) {
        id = null;
    } else {
        id = in.readLong();
    }
    webSafeKey = in.readString();
    productivity = in.readFloat();
    address = in.readString();
    areas = new ArrayList<Area>();
    in.readTypedList(areas,Area.CREATOR);
    //areas = in.readArrayList(null);
}
public void writeToParcel(Parcel out, int flags) {
    out.writeLong(id);
    out.writeString(webSafeKey);
    out.writeFloat(productivity);
    out.writeString(address);
    out.writeTypedList(areas);
}

构造函数调用in.readByte(),但从未在writeToParcel()方法中编写。您的writeToParcel()看起来应如下所示:

public void writeToParcel(Parcel out, int flags) {
    if (id != null) {
        out.writeByte((byte) 1);
        out.writeLong(id);
    } else {
        out.writeByte((byte) 0);
    }
    out.writeString(webSafeKey);
    out.writeFloat(productivity);
    out.writeString(address);
    out.writeTypedList(areas);
}

这显然不会导致OutOfMemoryError,但是,在尝试读取列表的大小并获取大量数据之前,可能会使分区数据“排列”得足够好,因为读取/写入是错位的。