[英]Error inflating class android.support.v7.widget.CardView
I unexpectedly encountered the following error while trying to run my application:我在尝试运行我的应用程序时意外遇到以下错误:
Binary XML file line #8: Error inflating class android.support.v7.widget.CardView
Below is the log cat:下面是日志猫:
11-09 13:11:58.558: E/AndroidRuntime(12542): FATAL EXCEPTION: main
11-09 13:11:58.558: E/AndroidRuntime(12542): android.view.InflateException: Binary XML file line #8: Error inflating class android.support.v7.widget.CardView
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-09 13:11:58.558: E/AndroidRuntime(12542): at com.dooba.beta.ThirdFragment.onCreateView(ThirdFragment.java:15)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.os.Handler.handleCallback(Handler.java:615)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.os.Handler.dispatchMessage(Handler.java:92)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.os.Looper.loop(Looper.java:137)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-09 13:11:58.558: E/AndroidRuntime(12542): at java.lang.reflect.Method.invokeNative(Native Method)
11-09 13:11:58.558: E/AndroidRuntime(12542): at java.lang.reflect.Method.invoke(Method.java:511)
11-09 13:11:58.558: E/AndroidRuntime(12542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-09 13:11:58.558: E/AndroidRuntime(12542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-09 13:11:58.558: E/AndroidRuntime(12542): at dalvik.system.NativeStart.main(Native Method)
11-09 13:11:58.558: E/AndroidRuntime(12542): Caused by: java.lang.ClassNotFoundException: android.support.v7.widget.CardView
11-09 13:11:58.558: E/AndroidRuntime(12542): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-09 13:11:58.558: E/AndroidRuntime(12542): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-09 13:11:58.558: E/AndroidRuntime(12542): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
11-09 13:11:58.558: E/AndroidRuntime(12542): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
11-09 13:11:58.558: E/AndroidRuntime(12542): ... 19 more
Below is the java code:下面是java代码:
public class ThirdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_city, container, false);
return rootView;
}
}
Below is the layout code:下面是布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
>
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
Thanks in advance提前致谢
I solved this by adding updated cardview and appcompat on the app/build.gradle我通过在 app/build.gradle 上添加更新的 cardview 和 appcompat 解决了这个问题
dependencies {
...
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
}
Then rebuild the project然后重建项目
the issue can also be caused by using the androidX
try to change to <androidx.cardview.widget.CardView
and the crashing should be ok.该问题也可能是由于使用
androidX
尝试更改为<androidx.cardview.widget.CardView
,并且崩溃应该没问题。 One tip, check if you see the layout in the preview.一个提示,检查您是否在预览中看到布局。
in the gradle it shoud be like : implementation 'androidx.cardview:cardview:1.0.0'
在gradle中它应该是这样的:
implementation 'androidx.cardview:cardview:1.0.0'
Just locate your cardview layout in your xml file, for example例如,只需在您的 xml 文件中找到您的 cardview 布局
<android.support.v7.widget.CardView......</android.support.v7.widget.CardView>
and replace it with并将其替换为
<androidx.cardview.widget.CardView.......</androidx.cardview.widget.CardView>
You need to specify a dependency on the v7 cardview library您需要指定对v7 cardview 库的依赖
Edit build.gradle and specify "com.android.support:cardview-v7:21.0.+" in the dependencies section.编辑 build.gradle 并在依赖项部分指定“com.android.support:cardview-v7:21.0.+”。
Answers above didn't help.上面的答案没有帮助。 Works fine after adding
添加后运行正常
<android.support.v7.widget.CardView
...
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardBackgroundColor="@android:color/transparent">
Can't explain.无法解释。
This error is caused because you are probably using AndroidX but you are using this dependency:导致此错误的原因是您可能正在使用 AndroidX 但您正在使用此依赖项:
implementation 'com.android.support:cardview-v7:27.1.1'
Change to AndroidX CardView dependency更改为 AndroidX CardView 依赖项
implementation 'androidx.cardview:cardview:1.0.0'
and change your class into the layout:并将您的课程更改为布局:
<androidx.cardview.widget.CardView
To fix this problem .要解决这个问题。 first you must add cardview from the
首先,您必须从
That works for me.这对我行得通。
如果在 AndroidX 中发生此错误,则需要使用以下依赖项来修复此错误:
implementation 'androidx.cardview:cardview:1.0.0'
problem is with your dependencies问题在于您的依赖项
try this read every step carefully试试这个仔细阅读每一步
https://developer.android.com/tools/support-library/setup.html#libs-with-res https://developer.android.com/tools/support-library/setup.html#libs-with-res
While checking all your dependency just check the related JAVA class file that is it extends AppCompatACtivity or not.在检查所有依赖项时,只需检查相关的 JAVA 类文件是否扩展 AppCompatACtivity 。 Sure this will help you.
当然这会帮助你。
You may get this error because you may have copied the code from a web page and pasted it as it is in your project especially in layouts.您可能会收到此错误,因为您可能已从网页中复制代码并将其粘贴到您的项目中,尤其是在布局中。 Please don't do that and if you do that you should see the design part if it is OK or not.I also got the same error and solved it by implementing by myself.
请不要这样做,如果您这样做,您应该看到设计部分是否正常。我也遇到了同样的错误,并通过自己实现解决了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.