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

后端 未结 15 1492
-上瘾入骨i
-上瘾入骨i 2020-11-27 20:47

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 clas         


        
相关标签:
15条回答
  • 2020-11-27 21:28

    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.

    0 讨论(0)
  • 2020-11-27 21:30

    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

    0 讨论(0)
  • 2020-11-27 21:30

    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 讨论(0)
  • 2020-11-27 21:35

    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.
    0 讨论(0)
  • 2020-11-27 21:37

    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

    0 讨论(0)
  • 2020-11-27 21:39

    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!

    0 讨论(0)
提交回复
热议问题