Android aidl 编译报couldn't find import for class

   Android进程间通信需要用到AIDL,AIDL默认支持的类型包括Java基本类型(int、long、boolean等),和(String、List、Map、CharSequence),如果要传递自定义的类型需要实现android.os.Parcelable接口。自己写了一个实体类public class StudentParcelable implements Parcelable。然后定义了AIDL接口AIDLStudent,但是报错:

查找资料发现,原来不仅要定义实现android.os.Parcelable接口的类,还得为该实现类定义一个aidl文件,如下:

Android aidl 编译报couldn't find import for class_第1张图片

这样AIDLStudent接口不就会报import错误了。

ps:附上StudentParcelable类的源码,注意字段写入Parcel和从Parcel读取的顺序一定要保持一致

package com.cnpc.aidl;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class StudentParcelable implements Parcelable {
	private static final String TAG = "StudentParcelable";

	private int age;
	private String name;
	private String address;
	private String phone;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public StudentParcelable() {
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public int describeContents() {
		Log.i(TAG, "describeContents");
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		Log.i(TAG, "writeToParcel");
		dest.writeString(name);
		dest.writeInt(age);
		dest.writeString(address);
		dest.writeString(phone);
	}

	public static final Parcelable.Creator<StudentParcelable> CREATOR = new Parcelable.Creator<StudentParcelable>() {
		@Override
		public StudentParcelable createFromParcel(Parcel in) {
			Log.i(TAG, "createFromParcel");
			// 注意:读取字段的顺序一定要和writeToParcel中写入的顺序一致!
			StudentParcelable student = new StudentParcelable();
			student.name = in.readString();
			student.age = in.readInt();
			student.address = in.readString();
			student.phone = in.readString();
			return student;
		}

		@Override
		public StudentParcelable[] newArray(int size) {
			Log.i(TAG, "newArray");
			return new StudentParcelable[size];
		}
	};

}


 

你可能感兴趣的:(android,aidl)