在 Android 中将 Parcelables 数组写入 Parcel
我正在尝试将实现 Parcelable 的对象数组写入Parcel 使用 writeParcelableArray。
我试图编写的对象被定义为(如您所期望的):
public class Arrival implements Parcelable {
/* All the right stuff in here... this class compiles and acts fine. */
}
我试图将它们写入“Parcel”:
@Override
public void writeToParcel(Parcel dest, int flags) {
Arrival[] a;
/* some stuff to populate "a" */
dest.writeParcelableArray(a, 0);
}
当 Eclipse 尝试编译它时,我收到错误:
绑定不匹配:通用方法 writeParcelableArray(T[], int) 类型 包裹不适用于 参数(到达[],int)。这 推断类型到达无效 替代有界参数 < T 扩展了 Parcelable >
我完全不明白这个错误消息。 Parcelable
是一个接口(而不是类),因此您无法扩展它。有人有什么想法吗?
更新:将 Parcelable
的 ArrayList
放入 Intent
时,我遇到了基本相同的问题:
Intent i = new Intent();
i.putParcelableArrayListExtra("locations", (ArrayList<Location>) locations);
产生:
Intent 类型中的 putParcelableArrayListExtra(String, ArrayList< ? extends Parcelable >) 方法不适用于参数 (String, ArrayList< Location >)
这可能是因为 Location
是我所在的类正在上面工作(包装 Arrival
s),但我不这么认为。
I'm trying to write an array of objects that implement Parcelable into a Parcel using writeParcelableArray.
The objects I'm trying to write are defined (as you'd expect) as:
public class Arrival implements Parcelable {
/* All the right stuff in here... this class compiles and acts fine. */
}
And I'm trying to write them into a `Parcel' with:
@Override
public void writeToParcel(Parcel dest, int flags) {
Arrival[] a;
/* some stuff to populate "a" */
dest.writeParcelableArray(a, 0);
}
When Eclipse tries to compile this I get the error:
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 >
I completely don't understand this error message. Parcelable
is an interface (not a class) so you can't extend it. Anyone have any ideas?
UPDATE: I'm having basically the same problem when putting an ArrayList
of Parcelable
s into an Intent
:
Intent i = new Intent();
i.putParcelableArrayListExtra("locations", (ArrayList<Location>) locations);
yields:
The method putParcelableArrayListExtra(String, ArrayList< ? extends Parcelable >) in the type Intent is not applicable for the arguments (String, ArrayList< Location >)
This may be because Location
was the class I was working on above (that wraps the Arrival
s), but I don't think so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,您可以扩展一个接口,而且看起来您需要这样做。 writeParcelableArray 中的泛型参数要求扩展接口(而不是接口本身)。尝试创建一个 MyParcelable 扩展 Parcelable 接口。然后使用接口声明您的数组,但 impl 应该是您的 Arrival extends MyParcelable。
Actually, you can extend an interface, and it looks like you need to do just that. The generics parameter in writeParcelableArray is asking for an extended interface (not the interface itself). Try creating an interface MyParcelable extends Parcelable. Then declaring your array using the interface, but the impl should be your Arrival extends MyParcelable.
事实证明,它只是想让我构建一系列 Parcelables。使用问题中的示例:
It turns out it just wanted me to build an array of Parcelables. To use the example from the question:
我知道问题已经解决,但我的解决方案是其他的,所以我将其发布在这里:在我的例子中,由于类名模糊(一些 dom.Comment 而不是我的 Comment 类),Eclipse 自动导入了错误的包。
I know the problem is solved but my solution was other so I'm posting it here: in my case Eclipse automatically imported wrong package because of classes names abiguity(some dom.Comment instead of my Comment class).