尝试返回自定义类对象时出现AIDL错误 [英] AIDL ERROR while trying to return custom class object

查看:233
本文介绍了尝试返回自定义类对象时出现AIDL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AIDL中的IPC传递响应"类对象.我使该类可拆分:

I am trying to pass 'Response' class object using IPC in AIDL. I have made the class parcelable:

public class Response implements Parcelable{
    private long id;
    private String speechString;
    private List<String> responseString = new ArrayList<String>();


    //set
    ...
    }

    //get
    ...

    public Response(Parcel in) {
        id = in.readLong();
        speechString = in.readString();
        if (in.readByte() == 0x01) {
            responseString = new ArrayList<String>();
            in.readList(responseString, String.class.getClassLoader());
        } else {
            responseString = null;
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(speechString);
        if (responseString == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(responseString);
        }
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Response createFromParcel(Parcel in) {
            return new Response(in);
        }

        public Response[] newArray(int size) {
            return new Response[size];
        }
    };
}

定义的Response.aidl:

Defined Response.aidl:

package com.example;

parcelable Response;

IappMain.aidl用于IPC,其定义如下:

IappMain.aidl is used for IPC and is defined as following:

package com.example;

// Declare any non-default types here with import statements
import com.example.Response;

interface IOizuuMain {
    int app(String aString);

    Response getResponseByString(String string);
}

但是在构建项目时,它在IappMain.java中给我以下错误:这行上的错误:不兼容的类型:无法将对象转换为响应"

but upon building the project, it gives me the following error in IappMain.java: "error: incompatible types: Object cannot be converted to Response" at this line:

_result = com.example.Response.CREATOR.createFromParcel(_reply);

推荐答案

此行所引起的错误:

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

类型参数需要同时添加到返回类型和正在创建的对象中.添加类型参数的更改是这样的:

Type parameters need to be added to both the return type and the object being created. The change to add type parameters is this:

public static final Parcelable.Creator<Response> CREATOR =
    new Parcelable.Creator<Response>() {

这篇关于尝试返回自定义类对象时出现AIDL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆