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

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.


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!


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!


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