ADT中ClassNotFoundException找不到CardView错误
开始使用sdk版本是4.3-api18,cardview是网上下载的library项目,各种addjar、order and export、addproject...
各种能搜到的方法都用了,仍旧是
Unable to start activity ComponentInfo{xxx.CardView}: android.view.InflateException: Binary XML file line #x: Error inflating class android.support.v7.widget.CardView
Caused by: android.view.InflateException: Binary XML file line #x: Error inflating class android.support.v7.widget.CardView
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.CardView
后经过高人指点,认为是sdk版本太低,因为Android 5.0才新增的RecyclerView和CardView,如下终于解决。
本文要求通过sdk更新了v7的cardview包,即%sdk%\extras\android\support\v7中下载了cardview包。
Library项目
1.从sdk中导入CardView
Import,
General,
Existing Projects into Workspace,
Select root directory,
从%sdk%\extras\android\support\v7中选择cardview,
注意勾选 Copy projects into workspace。
查看项目:
src-空,
gen-空,
assets-空,
libs-有android-support-v7-cardview.jar文件,
res-4个空drawable,
layout-空,
values非空(attrs.xml、colors.xml、dimens.xml、styles.xml)。
2.打开导入的cardview项目/project.properties文件:
target=android-18
android.library=true
target默认是当前sdk最小api(本例只有18和21)没有改变;android.library=true说明此项目默认作为一个Library项目。
3.右键项目,Build Project(执行2次)
执行第一次:
生成空的bin目录、
在gen目录生成包目录及BuildConfig.java、R.java文件。
执行第二次:
在bin目录中生成必要的文件(res-空、AndroidManifest.xml、cardview.jar-重要!、R.txt)。
引用项目
4.新建android项目,Target SDK选择21,Compile With选择21。
右键项目,Porperties,Android,Library中Add...,选择上面导入的cardview项目,Apply,OK。
查看project.properties文件:
1 2 3 |
target=android-21 android.library.reference.1=../cardview |
5.在布局文件中引用CardView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.CardView android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" card_view:cardCornerRadius="4dp" > <TextView android:id="@+id/info_text" android:layout_width="match_parent" android:layout_height="match_parent" android:text="CardView测试" /> </android.support.v7.widget.CardView> </RelativeLayout> |
6.此时可以运行项目
7.然后右键项目,Properties,Android,Project Build Target,修改目标版本为18(或修改project.properties文件中的target)。
此时也可以运行项目。
--Library项目不变,新项目创建时--------------------------
Target SDK 18
Compile With 21(对应project.properties/target=android-21)
可以运行
--Library项目不变,新项目创建时--------------------------
target sdk 18
compile with 18(对应project.properties/target=android-18)
可以运行
--Library项目不变,新项目创建时--------------------------
target sdk 21
compile with 18(对应project.properties/target=android-18)
可以运行
================================================================
最初 cardview library 包导入后将target修改为21,然后build2次,引用项目新建时target和compile都指定为21才运行成功。但记录本文时,library项目target是18,引用项目随便是几都可以,难道又不是sdk版本低的问题?...
也许关键是第一步Library项目的准备,另外可能和eclipse/adt有关。
谨以此文留个纪念吧。
奉上测试项目(sdk中纯cardview包、已build的library包+引用测试包):
[download id="1639"]
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/1633.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设