code_legend
code_legend

Reputation: 3285

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:

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

Upvotes: 27

Views: 38692

Answers (11)

Jorgesys
Jorgesys

Reputation: 126563

This error is caused because you are probably using AndroidX but you are using this dependency:

implementation 'com.android.support:cardview-v7:27.1.1'

Change to AndroidX CardView dependency

implementation 'androidx.cardview:cardview:1.0.0'

and change your class into the layout:

 <androidx.cardview.widget.CardView

Upvotes: 4

Reetu Jaiswal
Reetu Jaiswal

Reputation: 27

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.

Upvotes: 0

Wolfiebae
Wolfiebae

Reputation: 405

Just locate your cardview layout in your xml file, for example

<android.support.v7.widget.CardView......</android.support.v7.widget.CardView>

and replace it with

<androidx.cardview.widget.CardView.......</androidx.cardview.widget.CardView>

Upvotes: 9

Jerry Chong
Jerry Chong

Reputation: 9240

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

implementation 'androidx.cardview:cardview:1.0.0'

Upvotes: 1

Csabi
Csabi

Reputation: 3043

the issue can also be caused by using the androidX try to change to <androidx.cardview.widget.CardView and the crashing should be ok. 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'

Upvotes: 24

Mikhail
Mikhail

Reputation: 398

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.

Upvotes: 4

GauRav MisHra
GauRav MisHra

Reputation: 21

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

Upvotes: 0

Benson Macharia
Benson Macharia

Reputation: 1026

I solved this by adding updated cardview and appcompat on the app/build.gradle

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

Upvotes: 39

aleb
aleb

Reputation: 2562

You need to specify a dependency on the v7 cardview library

Edit build.gradle and specify "com.android.support:cardview-v7:21.0.+" in the dependencies section.

Upvotes: 4

Hemant Shori
Hemant Shori

Reputation: 2493

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.

That works for me.

Upvotes: 1

Munawwar Hussain Shelia
Munawwar Hussain Shelia

Reputation: 1364

problem is with your dependencies

try this read every step carefully

https://developer.android.com/tools/support-library/setup.html#libs-with-res

Upvotes: 0

Related Questions