在Android中将包裹数组写入一个包裹
我正在尝试使用writeParcelableArray将一个实现Parcelable的对象数组写入Parcel。在Android中将包裹数组写入一个包裹
我想写的对象定义(如你所期望)为:
public class Arrival implements Parcelable {
/* All the right stuff in here... this class compiles and acts fine. */
}
我试图把它们写为'包裹”有:
@Override
public void writeToParcel(Parcel dest, int flags) {
Arrival[] a;
/* some stuff to populate "a" */
dest.writeParcelableArray(a, 0);
}
当Eclipse试图编译这个我得到的错误:
Bound mismatch: The generic method writeParcelableArray(T[], int) of type Parcel is not applicable for the arguments (Arrival[], int). The inferred type Arrival is not a valid substitute for the bounded parameter < T extends Parcelable >
我完全不明白此错误消息。 Parcelable
是一个接口(不是类),所以你不能扩展它。有人有主意吗?
UPDATE:
Intent i = new Intent();
i.putParcelableArrayListExtra("locations", (ArrayList<Location>) locations);
产量:我的推杆Parcelable
秒的ArrayList
时为Intent
具有基本相同的问题
The method putParcelableArrayListExtra(String, ArrayList< ? extends Parcelable >) in the type Intent is not applicable for the arguments (String, ArrayList< Location >)
这可能是因为Location
是我上面正在做的课程(包装Arrival
s),但我不这么认为。
事实证明,它只是想让我建立一个可接受的数组。要使用从问题的例子:
@Override
public void writeToParcel(Parcel dest, int flags) {
Parcelable[] a;
/*
some stuff to populate "a" with Arrival
objects (which implements Parcelable)
*/
dest.writeParcelableArray(a, 0);
}
实际上,你可以扩展一个接口,看起来你需要这样做。 writeParcelableArray中的泛型参数是要求扩展接口(而不是接口本身)。尝试创建一个接口MyParcelable扩展Parcelable。然后用接口声明你的数组,但是impl应该是你的Arrival扩展MyParcelable。
我知道这个问题解决,但我的解决办法等,所以我在这里张贴:在我的情况下,Eclipse将自动导入,因为类名abiguity错包(DOM一些评论,而不是我的评论类)。
正如我所说,使用接口声明你的数组。很高兴你能解决问题。 – harschware 2009-12-12 22:09:06