Parcelable继承问题与继承人类getCreator [英] Parcelable Inheritance issue with getCreator of heir class
问题描述
我有一个parcelable类 A
和 B
扩展 A
I have a parcelable class A
and B
that extends A
例如:
A类
public abstract class A implements Parcelable {
private int a;
protected A(int a) {
this.a = a;
}
public int describeContents() {
return 0;
}
public static Creator<A> getCreator() {
return CREATOR;
}
public static final Parcelable.Creator<A> CREATOR = new Parcelable.Creator<A>() {
public A createFromParcel(Parcel in) {
return new A(in);
}
public A[] newArray(int size) {
return new A[size];
}
};
public void writeToParcel(Parcel out, int flags) {
out.writeInt(a);
}
protected A(Parcel in) {
a = in.readInt();
}
}
继承人B类
public class B extends A {
private int b;
public B(int a, int b) {
super(a);
this.b = b;
}
public static Creator<B> getCreator() {
return CREATOR;
}
public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>() {
public B createFromParcel(Parcel in) {
return new B(in);
}
public B[] newArray(int size) {
return new B[size];
}
};
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(b);
}
private B(Parcel in) {
super(in);
b = in.readInt();
}
}
在B I得到错误的的返回类型是A.getCreator()不兼容的的在
In B I get the error "the return type is incompatible with A.getCreator()" in
public static Creator<B> getCreator() {
return CREATOR;
}
显然,如果我尝试 getCreator
的类 B
的类型更改为造物主&LT; A方式&gt;
,不起作用,因为Parcelable创作者的类型是 B
clearly if I try to change the type of getCreator
of the class B
to Creator<A>
, doesn't work because the type of Parcelable creator is B
.
我怎么能解决这个问题呢?
how could I solve this issue?
推荐答案
下面是我如何实现这一点。我创建了一个抽象父类,允许使用的父类 A
,您应该增加两个抽象方法:
保护抽象无效writeChildParcel(包裹PC,诠释标志)
和保护抽象无效readFromParcel(包裹PC)
Here is how I implemented this. I created an abstract parent class, lets use your parent class A
, where you should add two abstract methods:
protected abstract void writeChildParcel(Parcel pc, int flags)
and protected abstract void readFromParcel(Parcel pc)
然后你需要一个静态方法来创建 A
的正确实例。在我的情况下,它是有意义的有一个类型的属性(可以使用枚举
),在那里我可以识别它们。这样我们就可以有一个静态的newInstance(整型)
方法,像这样:
Then you need a static method to create the right instance of A
. In my case it made sense to have a type attribute (you can use an enum
) where I could identify each of them. This way we can have a static newInstance(int type)
method like so:
public static A newInstance(int type) {
A a = null;
switch (type) {
case TYPE_B:
a = new B();
break;
...
}
return a;
}
public static A newInstance(Parcel pc) {
A a = A.newInstance(pc.readInt()); //
//call other read methods for your abstract class here
a.readFromParcel(pc);
return a;
}
public static final Parcelable.Creator<A> CREATOR = new Parcelable.Creator<A>() {
public A createFromParcel(Parcel pc) {
return A.newInstance(pc);
}
public A[] newArray(int size) {
return new A[size];
}
};
然后,写你的 writeToParcel
如下:
public void writeToParcel(Parcel out, int flags) {
out.writeInt(type);
//call other write methods for your abstract class here
writeChildParcel(pc, flags);
}
现在摆脱你的 CREATOR
和其他
Parcelable
方法乙的code>和刚实施 writeChildParcel
和 readFromParcel
。你应该是好去!
Now get rid of your CREATOR
and all other Parcelable
methods in B
and just implement writeChildParcel
and readFromParcel
in it. You should be good to go!
希望它帮助。
这篇关于Parcelable继承问题与继承人类getCreator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!