我正在尝试将任务打包成一个包,以便从我的服务传递到 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() 代替。有人可以确认这应该有效吗?下面的问题仍然有效。
此外,当 Activity 从 Intent 中读取任务时,我需要进行一些计算来填充其他一些数据成员。我可以调用什么函数来进行计算?是 readFromParcel(...) 还是以 Parcelable 作为参数的构造函数?
谢谢
最佳答案
toArray()
返回 Object[]
类型,这就是您得到的原因:
Object is not a valid substitute for the bounded parameter
Object 不扩展 Parcelable。您必须强制转换 toArray()
调用:
(Resources[])resources.toArray()
正如您所说,由于 Resources 实现了 Parcelable,这应该会消除您的异常。
关于android - 使类可打包错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3762476/