如何扩展实现Parcelable接口的android类?

时间:2013-07-18 14:08:31

标签: android inheritance android-location

首先,我检查this answer

我要做的是扩展名为Location的{​​{1}}类,其中有一些 成员变量。我想要实现的功能是将LocationPlus类的对象从一个活动传递到另一个活动。

这是我的LocationPlus

CREATOR

我面临的问题是这个错误

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

评论中有人要求我发布LocationPlus类,所以这里是

public LocationPlus(Parcel in) {

4 个答案:

答案 0 :(得分:34)

Parcelable,Speed King

根据 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");
 }

答案 1 :(得分:4)

嗨,我已经对此做了很多研究,但我找不到任何有用的东西。我尝试下面的解决方案,它对我有用。

假设您的超类只有名为“mData”的int变量。

public class Location implements Parcelable {
 protected int mData;

 public int describeContents() {
     return 0;
 }

 public void writeToParcel(Parcel out, int flags) {
     out.writeInt(mData);
 }

 public static final Parcelable.Creator<Location> CREATOR
         = new Parcelable.Creator<Location>() {
     public Location createFromParcel(Parcel in) {
         return new Location(in);
     }

     public Location[] newArray(int size) {
         return new Location[size];
     }
 };

 private Location(Parcel in) {
     mData = in.readInt();
 }

}

然后,你的扩展类只有名为“mBattery”的int变量。

public class LocationPlus extends Location {
 protected int mBattery;

 public int describeContents() {
     return 0;
 }

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

 public static final Parcelable.Creator<LocationPlus> CREATOR
         = new Parcelable.Creator<LocationPlus>() {
     public LocationPlus createFromParcel(Parcel in) {
         return new LocationPlus(in);
     }

     public LocationPlus[] newArray(int size) {
         return new LocationPlus[size];
     }
 };

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

}

到目前为止,LocationPlus工作正常。但是我们没有设置超类的变量。首先,我使用super(..)方法在扩展类上设置超类'变量。但它没有用。

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

而不是上面的代码,你应该明确地设置所有超类'变量。超类'变量应该受到保护。最终的构造函数应该是这样的:

private LocationPlus(Parcel in) {
     mData = in.readIn();
     mBattery = in.readInt();
 }

和writeToParcel方法应该是这样的:

public void writeToParcel(Parcel out, int flags) {
     out.writeIn(mData);
     out.writeInt(mBattery);
 }

答案 2 :(得分:1)

试试这个解决方案:

public static final Parcelable.Creator<LocationPlus> CREATOR =
    new Parcelable.Creator<LocationPlus>() {
    @Override
    public LocationPlus createFromParcel(Parcel in) {

        Location l = Location.CREATOR.createFromParcel(in);
        LocationPlus lp = new LocationPlus(l);

        lp.mBattery= in.readInt();

        return lp;
    }

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

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

}

答案 3 :(得分:0)

根据Android文档,Location类没有Location()构造函数。初始化LocationPlus课程时,您需要拨打super(String provider)super(Location l)

修改:更正语法

(见Location Android Doc