使类可打包错误
我正在尝试将任务打包成一个包,以便从我的服务传递到活动,但我在使用自定义类型的 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>
如果资源正在实现 Parcelable,那么我看不出问题出在哪里。
编辑:我相信我已经修复了这部分。我使用 .writeParcelableList() 代替。有人可以确认这应该有效吗?下面的问题仍然有效。
此外,当活动从意图中读取任务时,我需要进行一些计算来填充其他一些数据成员。我可以调用什么函数来进行计算?是 readFromParcel(...) 还是以 Parcelable 作为参数的构造函数?
谢谢
I am trying to make Task parcelable to put into an bundle to pass from my service to activity but I'm having a little bit of trouble working with the ArrayList of my custom type.
Task:
@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);
}
Resource:
@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);
}
It gives me an error on the prc.writeTypedArray function inside task:
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>
If Resources is implementing Parcelable then I don't see where the problem is.
Edit: I BELIEVE I FIXED THIS PART. I USED .writeParcelableList() INSTEAD. CAN SOMEONE CONFIRM THAT THIS SHOULD WORK? QUESTION BELOW IS STILL VALID.
Also when Task is read from the intent by the activity, I need to do some computation to fill some other data members. What function is called there that I can impliment to do the computation? Is it readFromParcel(...) or the constructor that takes in a Parcelable as a parameter?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
toArray()
返回一个Object[]
类型,这就是为什么你会得到:对象不扩展 Parcelable。您必须强制转换 toArray() 调用:
正如您所说,由于 Resources 实现了 Parcelable,因此这应该会消除您的异常。
toArray()
returns a type ofObject[]
, which is why you're getting:Object does not extend Parcelable. You must cast the
toArray()
call:As you said, since Resources implements Parcelable, this should get rid of your exception.