Java 错误膨胀类 android.support.v7.widget.CardView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26833517/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:22:51  来源:igfitidea点击:

Error inflating class android.support.v7.widget.CardView

javaandroidandroid-layoutandroid-activity

提问by John

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.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

提前致谢

回答by Munawwar Hussain Shelia

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

回答by Hemant Shori

To fix this problem . first you must add cardview from the

要解决这个问题。首先,您必须从

  1. Close the main project.
  2. Remove the android-support-v7-appcompat .
  3. Restart the Eclipse.
  4. Add the android-support-v7-appcompat .
  5. Build the project.
  6. Then open the main project and build all the three projects.
  7. The error still remains. Restart eclipse. That's it.
  1. 关闭主项目。
  2. 删除 android-support-v7-appcompat 。
  3. 重新启动 Eclipse。
  4. 添加 android-support-v7-appcompat 。
  5. 构建项目。
  6. 然后打开主项目并构建所有三个项目。
  7. 错误仍然存​​在。重启日食。就是这样。

That works for me.

这对我行得通。

回答by aleb

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.+”。

回答by macbee

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

然后重建项目

回答by GauRav MisHra

While checking all your dependency just check the related JAVA class file that is it extends AppCompatACtivityor not. Sure this will help you.

在检查所有依赖项时,只需检查相关的 JAVA 类文件是否扩展 AppCompatACtivity。当然这会帮助你。

回答by Mikhail

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.

无法解释。

回答by Csabi

the issue can also be caused by using the androidXtry to change to <androidx.cardview.widget.CardViewand the crashing should be ok. One tip, check if you see the layout in the preview.

该问题也可能是由使用androidX尝试更改为引起的,<androidx.cardview.widget.CardView并且崩溃应该没问题。一个提示,检查您是否在预览中看到布局。

in the gradle it shoud be like : implementation 'androidx.cardview:cardview:1.0.0'

在gradle中它应该是这样的: implementation 'androidx.cardview:cardview:1.0.0'

回答by Jerry Chong

If this error happened in AndroidX, you need to use following dependencies to fix this error:

如果在 AndroidX 中发生此错误,则需要使用以下依赖项来修复此错误:

implementation 'androidx.cardview:cardview:1.0.0'

回答by Wolfiebae

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>

回答by Reetu Jaiswal

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.

您可能会收到此错误,因为您可能已从网页中复制代码并将其粘贴到您的项目中,尤其是在布局中。请不要这样做,如果您这样做,您应该看到设计部分是否正常。我也遇到了同样的错误,并通过自己实现解决了它。