今天下午,在自己的项目中添加了对RecyclerView的支持,但是一直报出“Caused by: java.lang.NoClassDefFound
Error: android.support.v7.recyclerview.R$styleable”的异常,在网上搜罗了一遭,稀里糊涂一下午仍没有解决问题,最后自己尝试从最初加载类库入手找问题,结果最终找到的原因都让自己萌生了想把自己杀了的念头。
下面从头一一说起:
首先,建议大家在使用Google的Android Support Library时,不要采用直接将..\sdk\extras\android\m2repository
\com\android\support\recyclerview-v7中的xx.aar文件复制到libs目录,添加Build Path这种方式,还是导入Android 代码最官方;不知道如何导入的笔者参见Google官方链接:http://developer.android.com/tools/support-library/setup.html#libs-with-res。
其次,对于加载CardView,RecyclerView时报出的异常一种是android-support-v4.jar这个包版本不匹配,另一种就是在导完Android Support Library后,点击鼠标右键——Properties——Android——勾选Is Library——点击Apply。记住一定要点击Apply,应用当前改变,否则配置失效。我就是在导完RecyclerView包后,没点击Apply,结果一直报出上述红色标注的异常,困扰了我一下午。
最后将完整的导包实践罗列一下,方便大家查阅。
- Select File > Import.
- Select Existing Android Code Into Workspace and click Next.
- Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the
appcompat
project, browse to<sdk>/extras/android/support/v7/appcompat/
. - Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.
- In the Order and Export tab, check the
.jar
files you just added to the build path, so they are available to projects that depend on this library project. For example, theappcompat
project requires you to export both theandroid-support-v4.jar
andandroid-support-v7-appcompat.jar
files. - Right-click the library project folder and select Properties > check Is Library>click Apply.
- Click OK to complete the changes.
时间: 2024-10-05 04:45:01