欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

断网使用RycyclerView的jar包,运行时报错

程序员文章站 2022-06-05 19:36:34
...

我在android工程的libs文件中加入RecycerView的jar包,运行后出现以下错误:

05-23 11:40:47.179 30641-30641/com.example.fan.uibestpractice E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.fan.uibestpractice, PID: 30641
    java.lang.RuntimeException: Unable to start activity ComponentInfo

{com.example.fan.uibestpractice/com.example.fan.uibestpractice.MainActivity}: 

android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class 

android.support.v7.widget.RecyclerView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3303)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
     Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error 

inflating class android.support.v7.widget.RecyclerView
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class 

android.support.v7.widget.RecyclerView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:658)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:801)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.fan.uibestpractice.MainActivity.onCreate(MainActivity.java:21)
        at android.app.Activity.performCreate(Activity.java:7383)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411)
        at android.app.ActivityThread.-wrap12(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
     Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/recyclerview/R

$styleable;
        at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:691)
        at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:647)
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:658) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:801) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:874) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.example.fan.uibestpractice.MainActivity.onCreate(MainActivity.java:21) 
        at android.app.Activity.performCreate(Activity.java:7383) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3411) 
        at android.app.ActivityThread.-wrap12(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994) 
        at android.os.Handler.dispatchMessage(Handler.java:108) 
        at android.os.Looper.loop(Looper.java:166) 
        at android.app.ActivityThread.main(ActivityThread.java:7529) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 
    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.recyclerview.R

$styleable" on path: DexPathList[[zip file "/data/app/com.example.fan.uibestpractice-

vYH2dgzPueilqQNMspxfSw==/base.apk", zip file "/data/app/com.example.fan.uibestpractice-

vYH2dgzPueilqQNMspxfSw==/split_lib_dependencies_apk.apk", zip file 

"/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_resources_apk.apk", zip 

file "/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_slice_0_apk.apk", zip 

file "/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_slice_1_apk.apk", zip 

file "/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_slice_2_apk.apk", zip 

file "/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_slice_3_apk.apk", zip 

file "/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_slice_4_apk.apk", zip 

file "/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_slice_5_apk.apk", zip 

file "/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_slice_6_apk.apk", zip 

file "/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_slice_7_apk.apk", zip 

file "/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_slice_8_apk.apk", zip 

file "/data/app/com.example.fan.uibestpractice-

vYH2dgzPueilqQNMspxfSw==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=

[/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/lib/arm64, /system/lib64, 

/vendor/lib64, /product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        	... 27 more
    	Suppressed: java.io.IOException: No original dex files found for dex location 

/data/app/com.example.fan.uibestpractice-vYH2dgzPueilqQNMspxfSw==/split_lib_resources_apk.apk
        at dalvik.system.DexFile.openDexFileNative(Native Method)
        at dalvik.system.DexFile.openDexFile(DexFile.java:353)
        at dalvik.system.DexFile.<init>(DexFile.java:100)
        at dalvik.system.DexFile.<init>(DexFile.java:74)
        at dalvik.system.DexPathList.loadDexFile(DexPathList.java:374)
        at dalvik.system.DexPathList.makeDexElements(DexPathList.java:337)
        at dalvik.system.DexPathList.<init>(DexPathList.java:157)
        at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
        at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
        at com.android.internal.os.PathClassLoaderFactory.createClassLoader(PathClassLoaderFactory.java:43)
        at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:75)
        at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:38)
        at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:719)
        at android.app.LoadedApk.getClassLoader(LoadedApk.java:752)
        at android.app.LoadedApk.getResources(LoadedApk.java:1005)
        at android.app.ContextImpl.createAppContext(ContextImpl.java:2489)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6497)
        at android.app.ActivityThread.-wrap2(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        		... 6 more

看错误中

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.recyclerview.R

$styleable" on path:

可知由于编译时在路径中找不到"android.support.v7.recyclerview.R$styleable"报错的,查看我们的jar包已经导入成功,如下图所示:

断网使用RycyclerView的jar包,运行时报错

原来该路径指向的不是Libs文件夹中recyclerview的jar包的路径,而是程序默认的jar包路径。在app/build.grdle文件中引入recyclerview依赖:。

implementation 'com.android.support:recyclerview-v7:28.0.0'

如下图所示:

断网使用RycyclerView的jar包,运行时报错

注意版本号“28.0.0”要与你的项目中的appcompat-v7中的版本号保持一致,我的是28.0.0。此时我的电脑是断网状态的,点击【syn】同步app/buildgradle文件,程序将会把libs包中的recyclerview的jar包自动导入External Libraries目录,如下图:
断网使用RycyclerView的jar包,运行时报错

此时运行仍会报错,因为项目中有两个recyclerview文件资源,编译器在编译时不知道要选哪一个。我们只需要将libs里的recyclerview的jar包删除掉,即:删除下图中的classes.jar。

断网使用RycyclerView的jar包,运行时报错

点击编译,报错如下:

Transform output file D:\WorkSpace1\myDemo\UIBestPractice2\app\libs\classes.jar does not exist.

因为我们已经删除了classes.jar文件,但是没有在project structure的依赖包将它删掉,所以会报错,进入Project Structure将classes.jar依赖包删掉,再运行就没有任何错误了,接下来就可以正常使用recyclerview了。

step:1

断网使用RycyclerView的jar包,运行时报错

step2:

断网使用RycyclerView的jar包,运行时报错

由于我的工程所在的电脑不能联网,所有才使用离线jar包,有网的话直接在app/build.gradle中添加依赖:

implementation 'com.android.support:recyclerview-v7:28.0.0'

就可以用recyclerview了。

         最后,大家可能有个疑问,为什么recyclerview的jar包要放到External libraries路径中才能正常使用,一般离线jar包直接copy到libs文件夹中,再右键【add as library ...】就能用了。我觉得可能是因为Recyclerview涉及到UI操作,将jar包引入后,发现Recyclerview控件的后边存在一个下载的箭头,表示当前Recyclerview控件未下载,直接在布局中引用后运行就会报错。而在app/build.gradle中添加依赖后,Recyclerview控件的后边的下载箭头就消失了,表示Recyclerview控件已将载入到项目中,可以正常引用了。

 

断网使用RycyclerView的jar包,运行时报错