java 将 Parcelables 数组写入 Android 中的 Parcel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1890844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:24:05  来源:igfitidea点击:

Writing arrays of Parcelables to a Parcel in Android

javaandroidparcelableparcel

提问by Jeremy Logan

I'm trying to write an array of objects that implement Parcelableinto a Parcelusing writeParcelableArray.

我想写实现对象的数组Parcelable地块使用 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:

当 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 >

绑定不匹配:Parcel 类型的泛型方法 writeParcelableArray(T[], int) 不适用于参数 (Arrival[], int)。推断类型 Arrival 不是有界参数的有效替代 <T extends Parcelable >

I completely don't understand this error message. Parcelableis an interface (not a class) so you can't extend it. Anyone have any ideas?

我完全不明白这个错误信息。Parcelable是一个接口(不是一个类)所以你不能扩展它。有人有想法么?

UPDATE:I'm having basically the same problem when putting an ArrayListof Parcelables into an Intent:

更新:ArrayListof Parcelables 放入 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 >)

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

This may be because Locationwas the class I was working on above (that wraps the Arrivals), but I don't think so.

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

采纳答案by Jeremy Logan

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

事实证明,它只是想让我构建一个 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);
}

回答by harschware

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.

实际上,您可以扩展一个接口,看起来您只需要这样做。writeParcelableArray 中的 generics 参数要求扩展接口(不是接口本身)。尝试创建一个接口 MyParcelable 扩展 Parcelable。然后使用接口声明您的数组,但 impl 应该是您的 Arrival extends MyParcelable。

回答by toommm

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).

我知道问题已经解决,但我的解决方案是其他的,所以我在这里发布:在我的情况下,Eclipse 由于类名称歧义(一些 dom.Comment 而不是我的 Comment 类)自动导入了错误的包。