readTypedList выдает OutOfMemoryError при использовании Parcelable в Android - PullRequest
0 голосов
/ 03 мая 2018

У меня есть три модели следующим образом:

Модель координат

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 и пытаюсь прочитать его во втором Деятельности, 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)

Может кто-нибудь объяснить, где я ошибаюсь, неправильная реализация пользовательского класса Marshalling и Unmarshalling?

Я использовал следующие ссылки для написания этого кода:

1 Ответ

0 голосов
/ 03 мая 2018

В вашем классе 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, но, возможно, данные в пакетах «выстраиваются» достаточно хорошо, пока не попытаются прочитать размер списка и получить огромное число, потому что операции чтения / записи смещены.

...