您的位置:首页 > 编程语言 > Java开发

在Eclipse编译aidl文件中出现couldn't find import for class原因

2014-02-25 16:14 471 查看
最近正在研究aidl,出现了以下错误: couldn't find import for class 无法导入类!



IMyService.aidl 如图:



工程目录:



Student.java

[java] view
plaincopy

private int age;

private String name;

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

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

//

// @Override

// public Student[] newArray(int size) {

// // TODO Auto-generated method stub

// return new Student[size];

// }

//

// @Override

// public Student createFromParcel(Parcel source) {

// // TODO Auto-generated method stub

// return new Student(source);

// }

//

//

// };

public Student() {

}

public Student(Parcel pl) {

age = pl.readInt();

name = pl.readString();

}



public int describeContents() {

// TODO Auto-generated method stub

return 0;

}



public void writeToParcel(Parcel dest, int flags) {

// TODO Auto-generated method stub

dest.writeInt(age);

dest.writeString(name);

}



后来在网上查询了一下。问题终于解决了,其实还要感谢这位大哥!http://blog.csdn.net/jackyu613/archive/2010/11/16/6011564.aspx

具体解决办法如下:



参考《Android/OPhone开发完全讲义》第8章里的一段叙述:

“AIDL服务只支持有限的数据类型,因此,如果用AIDL服务传递一些复杂的数据就需要做更一步处理。AIDL服务支持的数据类型如下:

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

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

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

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

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

其中后两种数据类型需要使用import进行导入,将在本章的后面详细介绍。

传递不需要import的数据类型的值的方式相同。传递一个需要import的数据类型的值(例如,实现android.os.Parcelable接口的类)的步骤略显复杂。除了要建立一个实现android.os.Parcelable接口的类外,还需要为这个类单独建立一个aidl文件,并使用parcelable关键字进行定义。”

其实我们只要在新建一下Student.aidl文件对 Student这个类进行定义,就可以了。代码如下:

[c-sharp] view
plaincopy

/* //device/java/android/android/content/Intent.aidl

**

** Copyright 2007, The Android Open Source Project

**

** Licensed under the Apache License, Version 2.0 (the "License");

** you may not use this file except in compliance with the License.

** You may obtain a copy of the License at

**

** http://www.apache.org/licenses/LICENSE-2.0

**

** Unless required by applicable law or agreed to in writing, software

** distributed under the License is distributed on an "AS IS" BASIS,

** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

** See the License for the specific language governing permissions and

** limitations under the License.

*/

package com.android.aidl;

parcelable Student;



在这里切记 parcelable必须为小写,到此问题得到解决,这时目录结构如下:





补充一句,网上有一种“办法”,说是在frameworks/base/Android.mk中的LOCAL_AIDL_INCLUDES := $(FRAMEWORKS_BASE_J***A_SRC_DIRS)前加上LOCAL_AIDL_INCLUDES := 欲import的类所在位置。其实这种“办法”也是受上面所说的import规则限制的。一个没有实现Parcelable接口的类,这种“办法”也是无能为力的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: