在 Android 中将 Parcelables 数组写入 Parcel

发布于 2024-08-14 22:25:30 字数 1566 浏览 8 评论 0原文

我正在尝试将实现 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 是一个接口(而不是类),因此您无法扩展它。有人有什么想法吗?

更新:ParcelableArrayList 放入 Intent 时,我遇到了基本相同的问题:

Intent i = new Intent();
i.putParcelableArrayListExtra("locations", (ArrayList<Location>) locations);

产生:

Intent 类型中的 putParcelableArrayListExtra(String, ArrayList< ? extends Parcelable >) 方法不适用于参数 (String, ArrayList< Location >)

这可能是因为 Location 是我所在的类正在上面工作(包装 Arrivals),但我不这么认为。

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 Parcelables 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 Arrivals), but I don't think so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2024-08-21 22:25:30

实际上,您可以扩展一个接口,而且看起来您需要这样做。 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.

眉目亦如画i 2024-08-21 22:25:30

事实证明,它只是想让我构建一系列 Parcelables。使用问题中的示例:

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

It turns out it just wanted me to build an array of Parcelables. To use the example from the question:

@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);
}
_蜘蛛 2024-08-21 22:25:30

我知道问题已经解决,但我的解决方案是其他的,所以我将其发布在这里:在我的例子中,由于类名模糊(一些 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文