2

我正在尝试使 Task parcelable 放入一个包中以从我的服务传递到活动,但我在使用我的自定义类型的 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() 代替。有人可以确认这应该有效吗?下面的问题仍然有效。

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

谢谢

4

1 回答 1

3

toArray()返回一个类型Object[],这就是你得到的原因:

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

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

(Resources[])resources.toArray()

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

于 2010-09-21T16:57:34.327 回答