標籤:
今天下午,在自己的項目中添加了對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, the appcompat project requires you to export both theandroid-support-v4.jar and android-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.
Android:載入CardView,RecyclerView異常解決方案