错误膨胀类和 android.support.v7.widget.CardView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26494346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error inflating class and android.support.v7.widget.CardView
提问by Harsh Pokharna
I want to use CardView in my project, but when I run my application, I get the following error. I'm using Eclipse
.
我想在我的项目中使用 CardView,但是当我运行我的应用程序时,出现以下错误。我正在使用Eclipse
.
Error: Error inflating class and android.support.v7.widget.CardView
The graphical view of my xml file says 'The following classes could not be instantiated:
- android.support.v7.widget.CardView (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.'
Please help.
请帮忙。
This is the layout for my fragment where I have used CardView
这是我使用过的片段的布局 CardView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.log.MyContactsFragment" >
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cvContactDetails"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="?android:attr/selectableItemBackground"
android:clickable="false"
android:elevation="20dp" >
<TextView
android:id="@+id/tvContacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Info" />
<Button
android:id="@+id/bDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
<Button
android:id="@+id/bExport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Export to phone contacts" />
</android.support.v7.widget.CardView>
This is the layout for my activity.
这是我的活动的布局。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/flMainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="@+id/lvLeftDrawer"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:alpha="255"
android:background="#0B2161"
android:divider="@null"
android:choiceMode="singleChoice" >
</ListView>
<ListView
android:id="@+id/lvRightDrawer"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:alpha="255"
android:background="#0B2161"
android:divider="@null"
android:choiceMode="singleChoice" >
</ListView>
</android.support.v4.widget.DrawerLayout>
This is my logcat
这是我的日志
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.log/com.example.log.HomeScreenActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v7.widget.CardView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access0(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v7.widget.CardView
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.example.log.MyContactsFragment.onCreateView(MyContactsFragment.java:60)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
at android.app.Activity.performStart(Activity.java:5018)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
25 more
Caused by: java.lang.NoClassDefFoundError: android.support.v7.cardview.R$styleable
at android.support.v7.widget.CardView.initialize(CardView.java:203)
at android.support.v7.widget.CardView.<init>(CardView.java:101)
... 28 more
Someone who has encountered the same problem or solved it, please comment.
有遇到过同样问题或解决过的朋友,请留言。
回答by Harsh Pokharna
I guess I can answer my own question.
我想我可以回答我自己的问题。
Go to File -> Import -> Existing Android code into workspace --> Browse (Go to sdk/extras/android/support/v7/cardview) --> Click ok --> Click Finish
转至文件 -> 导入 -> 现有 Android 代码到工作区 --> 浏览(转至 sdk/extras/android/support/v7/cardview)--> 单击确定 --> 单击完成
Your project explorer will now show cardview as a project.
您的项目资源管理器现在将 cardview 显示为一个项目。
Right click on cardview project --> Properties --> Android(Left Pane) --> Enable isLibrary (tick the checkbox) --> Apply --> ok
右键单击cardview项目-->属性-->Android(左窗格)-->启用isLibrary(勾选复选框)-->应用-->确定
Now Right click on your project --> Properties --> Android(Left pane) --> Add (under library) --> cardview --> apply --> ok
现在右键单击您的项目--> 属性--> Android(左窗格)--> 添加(在库下)--> cardview --> 应用--> 确定
Now right click on your project again --> build path --> configure build path --> Under projects tab, add cardview
现在再次右键单击您的项目 --> 构建路径 --> 配置构建路径 --> 在项目选项卡下,添加 cardview
You are done.
你完成了。
回答by Arthur Wang
I guess your problem is not within your xml layout, but your build.gradle settings.
我猜你的问题不在于你的 xml 布局,而是你的 build.gradle 设置。
When you want to use support library for Recyclerview or CardView in lower API devices, you not only needs to import the v7 library, but also needs to import Recycler View and Card View support independently.
当你想在较低的API设备中使用Recyclerview或CardView的支持库时,不仅需要导入v7库,还需要独立导入RecyclerView和CardView支持。
Like this:
像这样:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.+'
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.android.support:support-v4:21.0.0'
}
This is noted by Google's documents here: https://developer.android.com/training/material/compatibility.html
谷歌的文档在这里指出了这一点:https: //developer.android.com/training/material/compatibility.html
See dependencies section.
请参阅依赖项部分。
Also, you can download google's sample of RecyclerView to digging out.
此外,您可以下载 google 的 RecyclerView 示例进行挖掘。
Hope this will help!
希望这会有所帮助!
回答by Piyush Kukadiya
In my case i have to do both the things add android.support.v7.widget.CardView
as a library project and also add + check it's jar file in java build path
在我的情况下,我必须同时添加android.support.v7.widget.CardView
作为库项目的内容,并添加 + 检查它的 jar 文件java build path
Go to File -> Import -> Existing Android code into workspace --> Browse (Go to sdk/extras/android/support/v7/cardview) --> Click ok --> Click Finish
Right click on cardview project --> Properties --> Android(Left Pane) --> Enable isLibrary (tick the checkbox) --> Apply --> ok
Right click on your project --> Properties --> Android(Left pane) --> Add (under library) --> cardview --> apply --> ok
right click on your project again --> build path --> configure build path -->under libraries-->add jar-->expand cardview-->expand libs-->select
android.support.v7.widget.CardView.jar
under order and export-->check
android.support.v7.widget.CardView.jar
-->click ok
转至文件 -> 导入 -> 现有 Android 代码到工作区 --> 浏览(转至 sdk/extras/android/support/v7/cardview)--> 单击确定 --> 单击完成
右键单击cardview项目-->属性-->Android(左窗格)-->启用isLibrary(勾选复选框)-->应用-->确定
右键单击您的项目--> 属性--> Android(左窗格)--> 添加(在库下)--> cardview --> 应用--> 确定
再次右键单击您的项目--> 构建路径--> 配置构建路径--> 在库下--> 添加jar--> 扩展cardview--> 扩展库--> 选择
android.support.v7.widget.CardView.jar
在订单和导出-->检查
android.support.v7.widget.CardView.jar
-->点击确定
回答by Steven Elliott
I wasted hours on this, for me the final piece of the puzzle was going to the "cardview" project that had been imported into Eclipse as an Android project, then right clicking > properties > android > and changing it from Android 2.1 to Lollipop!
我在这上面浪费了几个小时,对我来说,最后一块拼图是作为 Android 项目导入 Eclipse 的“cardview”项目,然后右键单击 > 属性 > android > 并将其从 Android 2.1 更改为 Lollipop!
回答by SureshCS50
if you are using intellij use these steps, this actually works
如果您使用的是 Intellij,请使用这些步骤,这实际上有效
- click in your project,
- right click -> open Module Settings (f4)
- import cardview from \sdk\extras\android\support\v7\
- add .jar file to cardview module
- click on your project and give module dependency to cardview
- now, click (+) button on cardview -> android -> at top you will see a check box (library module), enable it.
- click ok and close your settings dialog.
- rebuild your project and run it.
- 单击您的项目,
- 右键单击 -> 打开模块设置 (f4)
- 从 \sdk\extras\android\support\v7\ 导入 cardview
- 将 .jar 文件添加到 cardview 模块
- 单击您的项目并将模块依赖项赋予 cardview
- 现在,单击 cardview 上的 (+) 按钮 -> android -> 在顶部您将看到一个复选框(库模块),启用它。
- 单击确定并关闭您的设置对话框。
- 重建你的项目并运行它。
回答by Tink
I got error gone by Adding Dependencies
我通过添加依赖项得到了错误
The RecyclerView and CardView widgets are part of the v7 Support Libraries. To use these widgets in your project, add these Gradle dependencies to your app's module:
RecyclerView 和 CardView 小部件是 v7 支持库的一部分。要在您的项目中使用这些小部件,请将这些 Gradle 依赖项添加到您的应用程序模块中:
dependencies { ... compile 'com.android.support:cardview-v7:21.0.+' compile 'com.android.support:recyclerview-v7:21.0.+' }
依赖{ ...编译'com.android.support:cardview-v7:21.0.+' 编译'com.android.support:recyclerview-v7:21.0.+' }
http://developer.android.com/training/material/lists-cards.html
http://developer.android.com/training/material/lists-cards.html
回答by sverma
These steps work for me :)
这些步骤对我有用:)
Go to File -> Import -> Existing Android code into workspace --> Browse (Go to sdk/extras/android/support/v7/cardview) --> Click ok --> Click Finish
转至文件 -> 导入 -> 现有 Android 代码到工作区 --> 浏览(转至 sdk/extras/android/support/v7/cardview)--> 单击确定 --> 单击完成
Your project explorer will now show cardview as a project.
您的项目资源管理器现在将 cardview 显示为一个项目。
Right click on cardview project --> Properties --> Android(Left Pane) --> Enable isLibrary (tick the checkbox) --> Apply --> ok
右键单击cardview项目-->属性-->Android(左窗格)-->启用isLibrary(勾选复选框)-->应用-->确定
Now Right click on your project --> Properties --> Android(Left pane) --> Add (under library) --> cardview --> apply --> ok
现在右键单击您的项目--> 属性--> Android(左窗格)--> 添加(在库下)--> cardview --> 应用--> 确定
Now right click on your project again --> build path --> configure build path --> Under projects tab, add cardview
现在再次右键单击您的项目 --> 构建路径 --> 配置构建路径 --> 在项目选项卡下,添加 cardview
one more step you should require to remove error, that is order of cardview lib.
您还需要再执行一个步骤来消除错误,即 cardview lib 的顺序。
so right click on you project again --> properties --> Android(left pane)--> select cardview lib in library window --> and move to up
所以再次右键单击您的项目--> 属性--> Android(左窗格)--> 在库窗口中选择 cardview lib--> 并向上移动
回答by Mullazman
@sunil
@sunil
As said before:
正如之前所说:
Go to File -> Import -> Existing Android code into workspace --> Browse (Go to sdk/extras/android/support/v7/cardview) --> Click ok --> Click Finish
Right click on cardview project --> Properties --> Android(Left Pane) --> Enable isLibrary (tick the checkbox) --> Apply --> ok
Right click on your project --> Properties --> Android(Left pane) --> Add (under library) --> cardview --> apply --> ok
转至文件 -> 导入 -> 现有 Android 代码到工作区 --> 浏览(转至 sdk/extras/android/support/v7/cardview)--> 单击确定 --> 单击完成
右键单击cardview项目-->属性-->Android(左窗格)-->启用isLibrary(勾选复选框)-->应用-->确定
右键单击您的项目--> 属性--> Android(左窗格)--> 添加(在库下)--> cardview --> 应用--> 确定
If this doesn't work, proceed to add and remove individual jar's of the same package from your now imported code's library project's folder android-support-v7-cardview/libs
. Remove the Jar, clean the project and try it again. Remove the project one bloke suggested adding, remove any external Jar's related to cardview (I think these cause conflicts).
Clean the project and hopefully that works for you.
如果这不起作用,请继续从您现在导入的代码的库项目文件夹中添加和删除同一包的单个 jar android-support-v7-cardview/libs
。取出 Jar,清理项目并重试。删除一个家伙建议添加的项目,删除任何与 cardview 相关的外部 Jar(我认为这些会导致冲突)。清理项目,希望这对你有用。
Note:I personally did have to change the cardview library's target build level from 4.0.3to 5.0then back to 4.1.2, cleaning the project each time, for it to work as well.
注意:我个人确实必须将 cardview 库的目标构建级别从4.0.3更改为5.0 ,然后再更改回4.1.2,每次都清理项目,以使其正常工作。
回答by Hemant Shori
To fix this problem . first you must add cardview from the
要解决这个问题。首先,您必须从
- Close the main project.
- Remove the android-support-v7-appcompat .
- Restart the Eclipse.
- Add the android-support-v7-appcompat .
- Clean,To build the project.
- Then open the main project and build all the projects.
- The error still remains. Restart eclipse. That's it.
- 关闭主项目。
- 删除 android-support-v7-appcompat 。
- 重新启动 Eclipse。
- 添加 android-support-v7-appcompat 。
- 清洁,构建项目。
- 然后打开主项目并构建所有项目。
- 错误仍然存在。重启日食。就是这样。
That works for me.
这对我行得通。
回答by Nate Simpson
A related note: if you're using the support libraries, you're pretty sure your dependencies are correct, and you got here via a search for "Error inflating class CardView," then make sure your XML layout is using <android.support.v7.widget.CardView>and not just <CardView>!
相关说明:如果您正在使用支持库,则您非常确定您的依赖项是正确的,并且您通过搜索“错误膨胀类 CardView”到达这里,然后确保您的 XML 布局使用<android.support .v7.widget.CardView>而不仅仅是 <CardView>!
The two errors are pretty similar and it took me a few minutes to facepalm when I realized that I wasn't paying attention to the full class name in the error message and I was using the non-support <CardView> in my layout without realizing it.
这两个错误非常相似,当我意识到我没有注意错误消息中的完整类名并且我在我的布局中使用了不支持的 <CardView> 而没有意识到时,我花了几分钟的时间才意识到它。