25

I want to use CardView in my project, but when I run my application, I get the following error. I'm using 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

<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.access$600(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.

Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68
Harsh Pokharna
  • 1,121
  • 2
  • 9
  • 14

14 Answers14

56

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

Your project explorer will now show cardview as a project.

Right click on cardview project --> Properties --> Android(Left Pane) --> Enable isLibrary (tick the checkbox) --> Apply --> ok

Now Right click on your project --> Properties --> Android(Left pane) --> Add (under library) --> cardview --> apply --> ok

Now right click on your project again --> build path --> configure build path --> Under projects tab, add cardview

You are done.

Harsh Pokharna
  • 1,121
  • 2
  • 9
  • 14
  • 2
    I have the same problem. but... when I import CardView as a library project, It has not any java source file and the 'src' directory is empty. I examine the src folder in the sdk extra folder and it is empty! – Hamidreza Hosseinkhani Dec 27 '14 at 09:19
  • @HamidrezaHosseinkhani I had the same issue. Just ignore the empty src folder because the required file android-support-v7-cardview.jar is in the Android Private Libraries folder. Same goes for the RecyclerView. Answer works correctly. – Advait Saravade Jan 27 '15 at 14:58
  • 4
    I try to import CardView and RecyclerView library as the Eclipse Project instead of Android Project and IT DONE :) – Hamidreza Hosseinkhani Feb 16 '15 at 20:30
  • 6
    Just to add on to this answer, I followed these instructions step by step, but I still had this error. To solve this error, I went into the new cardview project and opened its `AndroidManifest.xml`. There, I went to the `uses-sdk` section and made sure that its `minSdkVersion` and `maxSdkVersion` matched *my* apps `minSdkVersion` and `maxSdkVersion`. That solved the error, after I followed these steps. – scottysseus Apr 12 '15 at 19:56
  • @ScottScooterWeidenkopf Thanks a lot dude. Was struggling with this stupid bug for almost 6 hours. – Anirudh Oct 15 '15 at 14:46
  • @ScottScooterWeidenkopf you might need to post it as new answer, your solution, point the correct one – Sruit A.Suk Mar 02 '16 at 20:01
40

I guess your problem is not within your xml layout, but your build.gradle settings.

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.

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

See dependencies section.

Also, you can download google's sample of RecyclerView to digging out.

Hope this will help!

Arthur Wang
  • 3,118
  • 4
  • 21
  • 29
12

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

  1. Go to File -> Import -> Existing Android code into workspace --> Browse (Go to sdk/extras/android/support/v7/cardview) --> Click ok --> Click Finish

  2. Right click on cardview project --> Properties --> Android(Left Pane) --> Enable isLibrary (tick the checkbox) --> Apply --> ok

  3. Right click on your project --> Properties --> Android(Left pane) --> Add (under library) --> cardview --> apply --> ok

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

  5. under order and export-->check android.support.v7.widget.CardView.jar-->click ok

Piyush Kukadiya
  • 1,880
  • 16
  • 26
4

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!

Steven Elliott
  • 3,214
  • 5
  • 29
  • 40
4

if you are using intellij use these steps, this actually works

  • 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.
SureshCS50
  • 3,608
  • 1
  • 20
  • 27
3

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:

dependencies { ... compile 'com.android.support:cardview-v7:21.0.+' compile 'com.android.support:recyclerview-v7:21.0.+' }

http://developer.android.com/training/material/lists-cards.html

Tink
  • 586
  • 7
  • 10
2

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

Your project explorer will now show cardview as a project.

Right click on cardview project --> Properties --> Android(Left Pane) --> Enable isLibrary (tick the checkbox) --> Apply --> ok

Now Right click on your project --> Properties --> Android(Left pane) --> Add (under library) --> cardview --> apply --> ok

Now right click on your project again --> build path --> configure build path --> Under projects tab, add cardview

one more step you should require to remove error, that is order of cardview lib.

so right click on you project again --> properties --> Android(left pane)--> select cardview lib in library window --> and move to up

sverma
  • 39
  • 5
1

@sunil

As said before:

  1. Go to File -> Import -> Existing Android code into workspace --> Browse (Go to sdk/extras/android/support/v7/cardview) --> Click ok --> Click Finish

  2. Right click on cardview project --> Properties --> Android(Left Pane) --> Enable isLibrary (tick the checkbox) --> Apply --> ok

  3. Right click on your project --> Properties --> Android(Left pane) --> Add (under library) --> cardview --> apply --> ok

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.

Note: I personally did have to change the cardview library's target build level from 4.0.3 to 5.0 then back to 4.1.2, cleaning the project each time, for it to work as well.

Community
  • 1
  • 1
Mullazman
  • 507
  • 5
  • 15
1

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. Clean,To build the project.
  6. Then open the main project and build all the projects.
  7. The error still remains. Restart eclipse. That's it.

That works for me.

Hemant Shori
  • 2,463
  • 1
  • 22
  • 20
0

import the CardView library as the Eclipse Project instead of the Android Project

0

I solved my problem by restarting the eclipse. Also make sure you have added the jars in build path and marked it for import/export.

0

This solution work on me.

1- Go to "sdk\extras\android\m2repository\com\android\support\recyclerview-v7*22.1.1*" ( 22.1.1 latest version for now. )

2- Open recyclerview-v7-22.1.1.aar file with Winrar.

3- There is a jar file named classes.jar. Copy that to your project libs folder.

4- Right click classes.jar, add to build path.

Thats it.

Mete
  • 2,805
  • 1
  • 28
  • 38
0

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

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.

Nate Simpson
  • 118
  • 1
  • 10
0

Disable the androidx from gradle.properties file using true --> false

android.useAndroidX=false

android.enableJetifier=false

then change these lines to api v28 in build.gradle(Module:app)

compileSdkVersion 28

targetSdkVersion 28

then add these line in to build.gradle(Module:app)

`implementation 'com.android.support:appcompat-v7:28.0.0'`
`implementation 'com.android.support:recyclerview-v7:28.0.0'`
`implementation 'com.android.support:cardview-v7:28.0.0'`
`implementation 'com.android.support:design:28.0.0'`

Then sync the project

kenlukas
  • 3,616
  • 9
  • 25
  • 36
HeshanHH
  • 653
  • 7
  • 9