【问题标题】:AIDL ERROR while trying to return custom class object尝试返回自定义类对象时出现 AIDL 错误
【发布时间】:2015-06-07 13:18:04
【问题描述】:

我正在尝试使用 AIDL 中的 IPC 传递“响应”类对象。我已将课程打包:

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:

package com.example;

parcelable Response;

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

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 中给了我以下错误: “错误:不兼容的类型:对象无法转换为响应”在这一行:

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

【问题讨论】:

  • 你真的需要 AIDL 吗?我的意思是,你真的需要远程服务吗?
  • 是的,因为我需要为我的应用程序的插件开发提供支持。
  • 它与远程服务有什么关系?
  • 插件作为远程服务安装
  • aidl中的String参数不需要in(不确定对你的问题有什么影响)

标签: java android service android-studio aidl


【解决方案1】:

错误是由这一行引起的:

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

类型参数需要添加到返回类型和正在创建的对象中。添加类型参数的变化是这样的:

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

【讨论】:

    【解决方案2】:

    尝试添加 公共响应() {}

    上面提到的代码。

     public Response(Parcel in) { .....
    

    .... }

    所以它应该看起来像

    public Response(){}
    

    公众响应(包裹){ ..... …… }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 2019-11-09
      相关资源
      最近更新 更多