首先我有支票this answer
我要做的是扩展名为Location的类
成员变量。我试图实现的功能是将LocationPlus类的对象从一个活动传递到另一个活动。
这是我的LocationPlus

public static final Parcelable.Creator<LocationPlus> CREATOR = new Parcelable.Creator<LocationPlus>() {
    @Override
    public LocationPlus createFromParcel(Parcel source) {
        return new LocationPlus(source);
    }
    @Override
    public LocationPlus[] newArray(int size) {
        return new LocationPlus[size];
    }
};

我面临的问题是这个错误
Implicit super constructor Location() is undefined. Must explicitly invoke another constructor

尝试编写构造函数时
public LocationPlus(Parcel in) {

有人在评论中要求我发布LocationPlus课程,所以这里是
public class LocationPlus extends Location{

    private int mBattery = -1;

    public LocationPlus(String locationName) {
        super(locationName);
    }

    public LocationPlus(Location location) {
        super(location);
    }

    public int getmBattery() {
        return mBattery;
    }

    public void setmBattery(int mBattery) {
        this.mBattery = mBattery;
    }
    @Override
    public int describeContents() {
        return 0;
    }

    public static final Parcelable.Creator<LocationPlus> CREATOR = new Parcelable.Creator<LocationPlus>() {
        @Override
        public LocationPlus createFromParcel(Parcel source) {
            return new LocationPlus(source);
        }
        @Override
        public LocationPlus[] newArray(int size) {
            return new LocationPlus[size];
        }
    };

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeInt(mBattery);
    }

    public LocationPlus(Parcel in) {
        mBattery =in.readInt();
    }
}

最佳答案

帕塞莱布尔,速度王
根据google engineers,此代码将运行得更快。其中一个原因是,我们对序列化过程是明确的,而不是使用反射来推断它。这也说明,代码已经为此进行了大量优化。

public abstract class BaseClass implements Parcelable {

    public String FullName;
    public boolean IsValidUser;
    public String UserName;


    public BaseClass () {
    }


    protected BaseClass(Parcel in) {
        FullName = in.readString();
        IsValidUser = in.readByte() != 0;
        UserName = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(FullName);
        dest.writeByte((byte) (IsValidUser ? 1 : 0));
        dest.writeString(UserName);
    }
}

子类将如下所示,使用列表添加到Parcelable对象中:
public class DerivedClass extends BaseClass {

    public boolean IsSuccess;
    public String Message;
    public List<AnotherClass> AnotherClassObj;


    public DerivedClass () {
        super();
    }

    protected DerivedClass(Parcel in) {
        super(in);
        AnotherClassObj = new ArrayList<AnotherClass>();
        IsSuccess = in.readByte() != 0;
        Message = in.readString();
        AnotherClassObj = in.readArrayList(AnotherClass.class.getClassLoader());
    }

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeByte((byte) (IsSuccess ? 1 : 0));
        dest.writeString(Message);
        dest.writeList(AnotherClassObj);
    }

    public int describeContents() {
        return 0;
    }
}

另一个儿童班:
public class AnotherClass extends BaseClass {
    public AnotherClass() {
        super();
    }

    protected AnotherClass(Parcel in) {
        super(in);
    }

    public int describeContents() {
        return 0;
    }

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

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

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

活动中:
 Intent intent = new Intent(LoginActivity.this, MainActivity.class);
 intent.putExtra("UserObject", parcelableObject);
 startActivity(intent);
 finish();

在接收活动中:
Bundle extras = getIntent().getExtras();
 if (extras != null) {
      userObject = extras.getParcelable("UserObject");
 }

07-28 12:54