使Class Parcelable错误

时间:2010-09-21 16:39:12

标签: android parcelable

我正在尝试将Task parcelable放入一个bundle以从我的服务传递给activity,但是我在使用我的自定义类型的ArrayList时遇到了一些麻烦。

任务:

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel prc, int arg1) {
    // TODO Auto-generated method stub
    prc.writeInt(id);
    prc.writeString(timeStamp_string);
    prc.writeString(timeToComplete_string);
    prc.writeTypedArray(resources.toArray(), PARCELABLE_WRITE_RETURN_VALUE);
}

资源:

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel prc, int flags) {
    // TODO Auto-generated method stub
    prc.writeInt(id);
    prc.writeString(timeStamp_string);
    prc.writeString(resourceType);
    prc.writeString(dataType);
    prc.writeString(value);
    prc.writeInt(taskId);
}

它给我一个任务内的prc.writeTypedArray函数错误:

Bound mismatch: The generic method writeTypedArray(T[], int) of type Parcel is not applicable for the arguments (Object[], int). The inferred type Object is not a valid substitute for the bounded parameter <T extends Parcelable>

如果Resources正在实施Parcelable,那么我看不出问题所在。

编辑:我相信我修复了这一部分。我使用了.writeParcelableList()INSTEAD。有人可以确认这应该工作吗?以下问题仍然有效。

当活动从意图中读取Task时,我需要做一些计算来填充其他一些数据成员。我可以在那里调用什么函数进行计算?是readFromParcel(...)还是将Parcelable作为参数的构造函数?

由于

1 个答案:

答案 0 :(得分:3)

toArray()会返回Object[]类型,这就是您收到的原因:

  

对象不是有界参数的有效替代

对象不扩展Parcelable。您必须投出toArray()电话:

(Resources[])resources.toArray()

正如你所说,由于Resources实现了Parcelable,这应该可以消除你的异常。