在引用RecyclerView类的时候编译不报错,运行时报错找不到类。
该情况只会在Eclipse中会出现,目测是ADT的bug:
04-07 14:34:00.045: E/AndroidRuntime(12739): FATAL EXCEPTION: main
04-07 14:34:00.045: E/AndroidRuntime(12739): Process: com.malata.android.font.fontmanagermaster, PID: 12739
04-07 14:34:00.045: E/AndroidRuntime(12739): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.malata.android.font.fontmanagermaster/com.malata.android.font.fontmanagermaster.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.v7.widget.RecyclerView
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.ActivityThread.access$800(ActivityThread.java:178)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.os.Handler.dispatchMessage(Handler.java:111)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.os.Looper.loop(Looper.java:194)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.ActivityThread.main(ActivityThread.java:5637)
04-07 14:34:00.045: E/AndroidRuntime(12739): at java.lang.reflect.Method.invoke(Native Method)
04-07 14:34:00.045: E/AndroidRuntime(12739): at java.lang.reflect.Method.invoke(Method.java:372)
04-07 14:34:00.045: E/AndroidRuntime(12739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
04-07 14:34:00.045: E/AndroidRuntime(12739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
04-07 14:34:00.045: E/AndroidRuntime(12739): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.v7.widget.RecyclerView
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.view.LayoutInflater.createView(LayoutInflater.java:633)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
04-07 14:34:00.045: E/AndroidRuntime(12739): at com.malata.android.font.fontmanagermaster.FontListFragment.onCreateView(FontListFragment.java:61)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.Fragment.performCreateView(Fragment.java:2069)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:899)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1072)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.BackStackRecord.run(BackStackRecord.java:852)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.Activity.performStart(Activity.java:6107)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
04-07 14:34:00.045: E/AndroidRuntime(12739): ... 10 more
04-07 14:34:00.045: E/AndroidRuntime(12739): Caused by: java.lang.reflect.InvocationTargetException
04-07 14:34:00.045: E/AndroidRuntime(12739): at java.lang.reflect.Constructor.newInstance(Native Method)
04-07 14:34:00.045: E/AndroidRuntime(12739): at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.view.LayoutInflater.createView(LayoutInflater.java:607)
04-07 14:34:00.045: E/AndroidRuntime(12739): ... 22 more
04-07 14:34:00.045: E/AndroidRuntime(12739): Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/recyclerview/R$styleable;
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:487)
04-07 14:34:00.045: E/AndroidRuntime(12739): at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:456)
04-07 14:34:00.045: E/AndroidRuntime(12739): ... 25 more
04-07 14:34:00.045: E/AndroidRuntime(12739): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.recyclerview.R$styleable" on path: DexPathList[[zip file "/data/app/com.malata.android.font.fontmanagermaster-2/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
04-07 14:34:00.045: E/AndroidRuntime(12739): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
04-07 14:34:00.045: E/AndroidRuntime(12739): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-07 14:34:00.045: E/AndroidRuntime(12739): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
04-07 14:34:00.045: E/AndroidRuntime(12739): ... 27 more
04-07 14:34:00.045: E/AndroidRuntime(12739): Suppressed: java.lang.ClassNotFoundException: android.support.v7.recyclerview.R$styleable
04-07 14:34:00.045: E/AndroidRuntime(12739): at java.lang.Class.classForName(Native Method)
04-07 14:34:00.045: E/AndroidRuntime(12739): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
04-07 14:34:00.045: E/AndroidRuntime(12739): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
04-07 14:34:00.045: E/AndroidRuntime(12739): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
04-07 14:34:00.045: E/AndroidRuntime(12739): ... 28 more
04-07 14:34:00.045: E/AndroidRuntime(12739): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
网上大部分帖子对于这个报错的解释是导入的包v4和recyclerview.jar不兼容,需要导入<sdk>\extras\android\support统一路径下的v4包和recyclerview。
发现是因为RecyclerView控件所依赖的android-support-v4包版本不一致造成的,即项目本身使用了一个appcompat的依赖(appcompat中包含了android-support-v4.jar),而这个support-v4和RecyclerView依赖的support-v4不是一个版本(从RecyclerView的源码中可以看到其确实使用了不少的support-v4工具类)
但我的情况仍然不起效,判断依然是包不兼容导致的。但不知为何导入同一个版本重新下载的包不起效。
在stackflow上找到另一种解决方案
- Go to C:\Users\Hk-PC\Desktop\eclipse\adt-bundle-windows-x86->20140702\sdk\extras\android\m2repository\com\android\support\recyclerview-v7\22.1.1
- Open recyclerview-v7-22.1.1.aar file with Winrar
- There is a jar file named classes.jar. Copy that to your project libs folder.
- Right click classes.jar, add to build path.
来源: http://stackoverflow.com/questions/29998850/recyclerview-class-not-found-in-eclipse
不要问我为何这种方式就可以了,而且这个方法复用性很低,就是说我把包删掉再按这个步骤来估计又会失效。网上资料几乎没有,用AS代替吧