使类可打包错误

发布于 2024-09-24 12:11:55 字数 1467 浏览 1 评论 0原文

我正在尝试将任务打包成一个包,以便从我的服务传递到活动,但我在使用自定义类型的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2024-10-01 12:11:55

toArray() 返回一个 Object[] 类型,这就是为什么你会得到:

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

对象不扩展 Parcelable。您必须强制转换 toArray() 调用:

(Resources[])resources.toArray()

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

toArray() returns a type of Object[], which is why you're getting:

Object is not a valid substitute for the bounded parameter

Object does not extend Parcelable. You must cast the toArray() call:

(Resources[])resources.toArray()

As you said, since Resources implements Parcelable, this should get rid of your exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文