Trouble getting Android Support V7 CardView Library to work

北城以北 提交于 2019-12-08 17:56:12

问题


I'm using IntelliJ/Android Studio with Ant build (not Gradle at the moment), and am trying to use android-support-v7-cardview.jar but I continue to get

android.view.InflateException: Binary XML file line #19: Error inflating class android.support.v7.widget.CardView
java.lang.NoClassDefFoundError: android.support.v7.cardview.R$styleable

I am not using m2repository with the aar file, but am using the cardview at location

sdk/extras/android/support/v7

I have gotten it to work by using the classes from the jar in my own project, but it requires API 21 (5.0) which I don't want to use yet. I am trying to use the jar file in the libs folder and res files from

 sdk/extras/android/support/v7/cardview/res 

I have added the lib file and res files to my own project, but still get the error messages above. What should I do?


回答1:


I had the exact same problem after adding compile 'com.android.support:cardview-v7:23.2.1' to build.gradle's dependencies to use the CardView, however after cleaning the project (Build > Clean Project) everything worked like magic.




回答2:


Hi this might be helpful, this worked for me. (for intellij users)

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



回答3:


I think you have to import it as an Android library project, then the ressources should be found correctly. I also tried first to import only the jar which didn't work, so I did it this way:

  1. The project for the CardView is located in your Android SDK folder: Android\android-sdk\extras\android\support\v7\cardview
  2. In Eclipse use "Import" and select "Existing Android Code Into Workspace" and import the CardView project
  3. Go to Properties->Android of the new project, mark it as a library project and then Link it as a library project in your own project where the CardView can not be found

That should do it




回答4:


I didn't figure out the error really but I just commented out the stuff that uses Android 5.0 in the source code of the library and it works fine. I didn't want to use a minSdk of Android 5.0.




回答5:


If you are using Android Studio then its quick easier, Please add compile 'com.android.support:cardview-v7:21.+' in dependencies in build.gradle.

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


来源:https://stackoverflow.com/questions/26826261/trouble-getting-android-support-v7-cardview-library-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!