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

From:http://blog.csdn.net/ghd2000/article/details/6082339

最近正在研究aidl,出现了以下错误: couldn't find import for class 无法导入类!

 

IMyService.aidl  如图:

工程目录:

Student.java

  1.  private int age;  
  2.     private String name;  
  3.     public int getAge() {  
  4.         return age;  
  5.     }  
  6.     public void setAge(int age) {  
  7.         this.age = age;  
  8.     }  
  9.     public String getName() {  
  10.         return name;  
  11.     }  
  12.     public void setName(String name) {  
  13.         this.name = name;  
  14.     }  
  15. //    public static final Parcelable.Creator<Student> CREATOR = new Creator<Student>() {   
  16. //   
  17. //        @Override   
  18. //        public Student[] newArray(int size) {   
  19. //            // TODO Auto-generated method stub   
  20. //            return new Student[size];   
  21. //        }   
  22. //   
  23. //        @Override   
  24. //        public Student createFromParcel(Parcel source) {   
  25. //            // TODO Auto-generated method stub   
  26. //            return new Student(source);   
  27. //        }   
  28. //   
  29. //         
  30. //    };   
  31.     public Student() {  
  32.     }  
  33.     public Student(Parcel pl) {  
  34.         age = pl.readInt();  
  35.         name = pl.readString();  
  36.     }  
  37.       
  38.     public int describeContents() {  
  39.         // TODO Auto-generated method stub   
  40.         return 0;  
  41.     }  
  42.       
  43.     public void writeToParcel(Parcel dest, int flags) {  
  44.         // TODO Auto-generated method stub   
  45.         dest.writeInt(age);  
  46.         dest.writeString(name);  
  47.     }  

 

后来在网上查询了一下。问题终于解决了,其实还要感谢这位大哥!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 plain copy print ?
  1. /* //device/java/android/android/content/Intent.aidl 
  2. ** 
  3. ** Copyright 2007, The Android Open Source Project 
  4. ** 
  5. ** Licensed under the Apache License, Version 2.0 (the "License");  
  6. ** you may not use this file except in compliance with the License.  
  7. ** You may obtain a copy of the License at  
  8. ** 
  9. **     http://www.apache.org/licenses/LICENSE-2.0  
  10. ** 
  11. ** Unless required by applicable law or agreed to in writing, software  
  12. ** distributed under the License is distributed on an "AS IS" BASIS,  
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  14. ** See the License for the specific language governing permissions and  
  15. ** limitations under the License. 
  16. */  
  17. package com.android.aidl;  
  18. parcelable Student;  

 

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

 

补充一句,网上有一种“办法”,说是在frameworks/base/Android.mk中的LOCAL_AIDL_INCLUDES := $(FRAMEWORKS_BASE_JAVA_SRC_DIRS)前加上LOCAL_AIDL_INCLUDES := 欲import的类所在位置。其实这种“办法”也是受上面所说的import规则限制的。一个没有实现Parcelable接口的类,这种“办法”也是无能为力的。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值