[英]Ant, Error inflating class android.support.v7.widget.RecyclerView
Let's create a clean new project: 让我们创建一个干净的新项目:
$ mkdir ApiTest
$ android create project --target android-22 --name ApiTest \
--path ./ApiTest --activity MyActivity --package com.example.apitest
Only add RecyclerView
to layout so res/layout/main.xml
looks: 仅将RecyclerView
添加到布局中,这样res/layout/main.xml
看起来如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Copy support libraries from ${ANDROID_SDK_PATH}/extras/android/support/v7/
: 从${ANDROID_SDK_PATH}/extras/android/support/v7/
复制支持库:
$ ls ApiTest/libs/
android-support-v4.jar android-support-v7-cardview.jar
android-support-v7-appcompat.jar android-support-v7-recyclerview.jar
Build & install: 构建并安装:
$ ant debug
$ adb install -r bin/ApiTest-debug.apk
And when I open the Activity exception is raised: 当我打开Activity异常时:
09-28 20:02:02.346: E/AndroidRuntime(28688): FATAL EXCEPTION: main
09-28 20:02:02.346: E/AndroidRuntime(28688): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apitest/com.example.apitest.MyActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView
09-28 20:02:02.346: E/AndroidRuntime(28688): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2357)
09-28 20:02:02.346: E/AndroidRuntime(28688): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
...
What is wrong here? 怎么了
I compile on 64bit Fedora 21 using openjdk java compiler: 我使用openjdk java编译器在64位Fedora 21上进行编译:
$ java -version
openjdk version "1.8.0_60"
OpenJDK Runtime Environment (build 1.8.0_60-b27)
OpenJDK 64-Bit Server VM (build 25.60-b23, mixed mode)
I overlooked that there are two types of Android Support Libraries. 我忽略了两种类型的Android支持库。 Libraries without resources (eg android-support-v4 ) and libraries with resources (eg appcompat and recyclerview ). 没有资源的库(例如android-support-v4 )和有资源的库(例如appcompat和recyclerview )。 See Adding Support Libraries . 请参阅添加支持库 。
To add recyclerview library (with resources) using command line and ant build tool first copy whole recyclerview directory from Android SDK directory to your project directory and update ant build files 要使用命令行和ant生成工具添加recyclerview库(包含资源),请先将整个recyclerview目录从Android SDK目录复制到您的项目目录中,然后更新ant生成文件
$ cd ${ANDROID_PROJECT_DIR}
$ cp -r ${ANDROID_SDK_DIR}/extras/android/support/v7/recyclerview/ .
$ android update lib-project --path recyclerview --target android-22
$ android update project --path . -l recyclerview
The third command should add line android.library=true
to recyclerview/project.properties
which in my case didn't so I added it manually. 第三个命令应将android.library=true
行添加到recyclerview/project.properties
,在我的情况下没有,因此我手动添加了它。 When I tried it with appcompat library it work as expected. 当我尝试使用appcompat库时,它可以按预期工作。
Another thing is that in the recyclerview/src
directory was missing. 另一件事是,缺少recyclerview/src
目录。 So simply create 所以只需创建
$ mkdir recyclerview/src
And that is it. 就是这样。 Now it compiles and runs. 现在,它可以编译并运行。
$ ant clean && ant debug && adb install -r bin/ApiTest-debug.apk
These links helped me: 这些链接帮助我:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.