1. 程式人生 > >報錯:Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView

報錯:Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView

最近學習RecyclerView,使用eclipse引用RecyclerView,編寫完demo後編譯沒有問題,一執行就掛掉,錯誤如下:

07-22 23:05:34.553: D/AndroidRuntime(2968): Shutting down VM
07-22 23:05:34.560: E/AndroidRuntime(2968): FATAL EXCEPTION: main
07-22 23:05:34.560: E/AndroidRuntime(2968): Process: com.zh.myrecyclerview, PID: 2968
07-22 23:05:34.560: E/AndroidRuntime(2968): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zh.myrecyclerview/com.zh.myrecyclerview
.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.app.ActivityThread.access$800(ActivityThread.java:144)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.os.Handler.dispatchMessage(Handler.java:102)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.os.Looper.loop(Looper.java:135)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.app.ActivityThread.main(ActivityThread.java:5221)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at java.lang.reflect.Method.invoke(Native Method)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at java.lang.reflect.Method.invoke(Method.java:372)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
07-22 23:05:34.560: E/AndroidRuntime(2968): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.view.LayoutInflater.createView(LayoutInflater.java:633)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.app.Activity.setContentView(Activity.java:2144)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at com.zh.myrecyclerview.MainActivity.onCreate(MainActivity.java:20)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.app.Activity.performCreate(Activity.java:5933)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	... 10 more
07-22 23:05:34.560: E/AndroidRuntime(2968): Caused by: java.lang.reflect.InvocationTargetException
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at java.lang.reflect.Constructor.newInstance(Native Method)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.view.LayoutInflater.createView(LayoutInflater.java:607)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	... 21 more
07-22 23:05:34.560: E/AndroidRuntime(2968): Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/recyclerview/R$styleable;
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:434)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:404)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	... 24 more
07-22 23:05:34.560: E/AndroidRuntime(2968): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.recyclerview.R$styleable" on path: DexPathList[[zip file "/data/app/com.zh.myrecyclerview-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
07-22 23:05:34.560: E/AndroidRuntime(2968): 	... 26 more
07-22 23:05:34.560: E/AndroidRuntime(2968): 	Suppressed: java.lang.ClassNotFoundException: android.support.v7.recyclerview.R$styleable
07-22 23:05:34.560: E/AndroidRuntime(2968): 		at java.lang.Class.classForName(Native Method)
07-22 23:05:34.560: E/AndroidRuntime(2968): 		at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
07-22 23:05:34.560: E/AndroidRuntime(2968): 		at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
07-22 23:05:34.560: E/AndroidRuntime(2968): 		at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
07-22 23:05:34.560: E/AndroidRuntime(2968): 		... 27 more
07-22 23:05:34.560: E/AndroidRuntime(2968): 	Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
07-22 23:05:37.406: I/Process(2968): Sending signal. PID: 2968 SIG: 9


試了很多方法都不行,整了半天,終於搞定,原來在eclipse中使用RecyclerView只能匯入API21.0.0中的包才可以使用。

解決辦法:進入sdk\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0目錄,找到recyclerview-v7-21.0.0.aar這個檔案。用解壓軟體解壓此檔案裡面有classes.jar,把這個jar包放到專案中的libs檔案下即可。這個jar包就是RecyclerView要使用的,可以給它改個名字例如android-support-v7-recyclerview.jar , 便於檢視。


最近升級了SDK發現sdk\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0這個目錄找不到了,所以把之前的jar包傳到了csdn的伺服器上,需要的可以到這下載:

注意注意注意:我提供的這個包是15年5.0剛釋出的時候保留的包,當時recyclerview這個控制元件也是剛剛推出,功能很強大,但是也有許多不完善的地方,在後期的sdk更新中官方可能更新了recyclerviewapi,使其更加完善。但這也導致大家在用我的包並在21以上的sdk編譯時有些方法不支援,比如 addOnScrollListener和android.support.v7.widget.helper.ItemTouchHelper

如果你沒有用到這些方法又必須使用eclipse可以繼續使用我的包,如果必須用這些方法,建議大家放棄使用eclipse,換成使用as開發。