zoukankan      html  css  js  c++  java
  • Android开发-解决 AIDL 中找不到couldn't find import for class错误

    最近在使用AIDL做IPC的时候,在处理复杂的数据类型的时候,编译器总是报couldn't find import for class错误,所以在这里总结下AIDL使用的时候的一些注意事项,希望对你能有所帮助。

    Android 中进程间通信使用了 AIDL 语言,但是支持的数据类型有限:

    1.Java的简单类型(int、char、boolean等)。不需要导入(import)。

    2.String和CharSequence。不需要导入(import)。

    3.List和Map。但要注意,List和Map对象的元素类型必须是AIDL服务支持的数据类型。不需要导入(import)。

    4.AIDL自动生成的接口。需要导入(import)。

    5.实现android.os.Parcelable接口的类。需要导入(import)。

    其中后两种数据类型需要使用import进行导入。

    刚开始想当然的以为传递Object只要实现了Parcelable接口在AIDL文件中导入即可, 然后编译器马上打败了我的天真,couldn't find import for class!!!,好吧还是老老实实的去看文档吧。AIDL文档链接

    解决这个错误的步骤如下:

    1.确保你的类已经实现了 Parcelable接口

    2.实现writeToParcel方法,将对象的当前状态写入 Parcel

    3.添加一个叫 CREATOR 的静态字段,它实现了 Parcelable.Creator

    4.最后创建一个对应的*.aidl文件去声明你的Parcelable类

    例如

    package android.graphics;
    
    // Declare Rect so AIDL can find it and knows that it implements
    // the parcelable protocol.
    parcelable Rect;
    import android.os.Parcel;
    import android.os.Parcelable;
    
    public final class Rect implements Parcelable {
        public int left;
        public int top;
        public int right;
        public int bottom;
    
        public static final Parcelable.Creator<Rect> CREATOR = new
    Parcelable.Creator<Rect>() {
            public Rect createFromParcel(Parcel in) {
                return new Rect(in);
            }
    
            public Rect[] newArray(int size) {
                return new Rect[size];
            }
        };
    
        public Rect() {
        }
    
        private Rect(Parcel in) {
            readFromParcel(in);
        }
    
        public void writeToParcel(Parcel out) {
            out.writeInt(left);
            out.writeInt(top);
            out.writeInt(right);
            out.writeInt(bottom);
        }
    
        public void readFromParcel(Parcel in) {
            left = in.readInt();
            top = in.readInt();
            right = in.readInt();
            bottom = in.readInt();
        }
    }
  • 相关阅读:
    iOS数据持久化的方式
    Runtime
    <02>
    <01>
    <02>
    UIActivityIndicatorView
    <01>数据存储
    UI<10>
    UI<09>
    UI<08>
  • 原文地址:https://www.cnblogs.com/thinkfeed/p/3261910.html
Copyright © 2011-2022 走看看