变量的值在Parcelable对象中的构造函数外部丢失

时间:2019-07-25 23:07:48

标签: java android parcelable

String变量finalMapSearchUrl与构造函数中的另一个StringmapUrlParam)串联,并且Log显示期望值:Final map URL : https://www.google.com/maps/search/?api=1&query=Spilia%20Beach(此处为mapUrlParam = Spilia Beach)。但是,当我从类外部调用getMapSearchUrl()方法并监视Log时,finalMapSearchUrl的值现在恢复为默认的https://www.google.com/maps/search/?api=1&query=。登录getMapSearchUrl()finalMapSearchUrl = https://www.google.com/maps/search/?api=1&query=。关于何时,为什么以及如何使用它的值的任何想法都不会保留在构造函数之外?

PlaceObject.java类:

public class PlaceObject implements Parcelable { // Implementing the Parcelable interface to allow for cleaner and faster code

    private static final String TAG = PlaceObject.class.getSimpleName();
    private static final String baseMapSearchUrl = "https://www.google.com/maps/search/?api=1&query="; // Base url for launching a Map activity with a Search Intent

    // Using int so that the values can be accessed via R.string etc.
    private int name;
    private int description;
    private int category;
    private String locationDistance;
    private String finalMapSearchUrl = baseMapSearchUrl;

    PlaceObject(int name, int description, int category , String locationDistance, String mapUrlParam) {
        this.name = name;
        this.description = description;
        this.locationDistance = locationDistance;
        this.category = category;
        finalMapSearchUrl += Uri.encode(mapUrlParam);
        Log.d(TAG,"Final map URL : " + finalMapSearchUrl);
    }

    private PlaceObject(Parcel in) {
        name = in.readInt();
        description = in.readInt();
        locationDistance = in.readString();
        category = in.readInt();
    }

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

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

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(name);
        parcel.writeInt(description);
        parcel.writeString(locationDistance);
        parcel.writeInt(category);
    }

    public int getName() {
        return name;
    }

    public int getDescription() {
        return description;
    }

    public String getLocationDistance() {
        return locationDistance;
    }

    public int getCategory() {
        return category;
    }

    public String getMapSearchUrl() {
        Log.d(TAG,"finalMapSearchUrl = " + finalMapSearchUrl);
        return finalMapSearchUrl; //TODO:sp figure out why the variable's value gets lost after the constructor is done
    }
} 

1 个答案:

答案 0 :(得分:2)

因为您只是获取基本网址,而不是一个User?

解决方案:

将其添加到包裹中并注意书写顺序,

赞:

Parceled

也不要忘记修复此问题:

private PlaceObject(Parcel in) {
        name = in.readInt();
        description = in.readInt();
        category = in.readInt();
        locationDistance = in.readString();
        finalMapSearchUrl = in.readString(); 
    }