java在Android中将包裹数组写入包裹
我正在尝试使用writeParcelableArray将实现Parcelable的对象数组写入Parcel
我尝试编写的对象定义(如您所料)如下:
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
是一个接口(不是类),因此不能扩展它。有人有什么想法吗
更新:我在将ArrayList
个Parcelable
放入Intent
时遇到了基本相同的问题:
Intent i = new Intent();
i.putParcelableArrayListExtra("locations", (ArrayList<Location>) locations);
收益率:
The method putParcelableArrayListExtra(String, ArrayList< ? extends Parcelable >) in the type Intent is not applicable for the arguments (String, ArrayList< Location >)
这可能是因为Location
是我在上面工作的类(它包装了Arrival
),但我不这么认为
# 1 楼答案
实际上,你可以扩展一个接口,看起来你需要这样做。writeParcelableArray中的泛型参数要求一个扩展接口(而不是接口本身)。尝试创建一个接口MyParcelable扩展Parcelable。然后使用接口声明数组,但impl应该是您的Arrival extends MyParcelable
# 2 楼答案
我知道问题已经解决了,但我的解决方案是另一个,所以我把它发布在这里:在我的例子中,Eclipse自动导入了错误的包,因为类名是abiguity(一些dom.Comment而不是我的Comment类)
# 3 楼答案
结果它只是想让我制作一系列的包裹。使用问题中的示例: