写作parcelables的阵列来一个包裹在Android中 [英] Writing arrays of Parcelables to a Parcel in Android
问题描述
我想写实现 Parcelable 成<一个对象的数组HREF =http://developer.android.com/reference/android/os/Parcel.html>包裹使用<一个href="http://developer.android.com/reference/android/os/Parcel.html#writeParcelableArray%28T%5B%5D,%20int%29">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);
}
在Eclipse中尝试编译这个我得到的错误:
When Eclipse tries to compile this I get the error:
约束不匹配:通用方法 类型writeParcelableArray(T [],INT) 包裹是不适用于 参数(港[],INT)。该 推断的类型到达不是有效 代替界参数 &LT;牛逼扩展Parcelable>
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
是一个接口(而不是类),所以你不能扩展它。任何人有什么想法?
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?
更新:我把一个的ArrayList
Parcelable $ C $当具有基本相同的问题C> s转换为
意图
:
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);
收益率:
该方法putParcelableArrayListExtra(字符串,ArrayList的&LT;延伸Parcelable>)的类型意图是不适用的参数(字符串,ArrayList的&LT;位置>)
The method putParcelableArrayListExtra(String, ArrayList< ? extends Parcelable >) in the type Intent is not applicable for the arguments (String, ArrayList< Location >)
这可能是因为位置
是我的工作上面(封装了抵达
S)类,但我不这么认为。
This may be because Location
was the class I was working on above (that wraps the Arrival
s), but I don't think so.
推荐答案
事实证明,它只是想让我打造parcelables的数组。要使用示例的问题:
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);
}
这篇关于写作parcelables的阵列来一个包裹在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!