14

首先,我检查了这个答案

我想做的是扩展Location调用它的类,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

尝试编写构造函数时

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();
    }
}
4

4 回答 4

37

Parcelable,速度之王

根据谷歌工程师的说法,这段代码的运行速度会明显更快。造成这种情况的原因之一是我们对序列化过程是明确的,而不是使用反射来推断它。也有理由为此目的对代码进行了大量优化。

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);
    }
}

子类将如下使用列表添加到可打包对象中:

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");
 }
于 2016-02-07T06:18:35.533 回答
5

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

假设您的超类只有名为“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);
 }
于 2014-12-09T18:04:32.683 回答
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);

}
于 2014-04-23T16:32:37.157 回答
0

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

编辑:更正语法

(请参阅位置 Android 文档

于 2013-07-18T14:15:51.653 回答