无法充气 Recyclerview

Cannot Inflate Recyclerview

提问人:Ary Parker 提问时间:8/18/2019 最后编辑:Gabriele MariottiAry Parker 更新时间:8/19/2019 访问量:144

问:

我的应用程序运行强制关闭android AVD,这是来自logcat的消息

I debugged and got this
08-18 00:14:14.497 3524-3524/com.example.goodresto E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.goodresto, PID: 3524
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.goodresto/com.example.goodresto.MainMenu}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class android.support.v7.widget.RecyclerView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class android.support.v7.widget.RecyclerView
        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.goodresto.MainMenu.onCreate(MainMenu.java:28)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.v7.widget.RecyclerView
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        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 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.example.goodresto.MainMenu.onCreate(MainMenu.java:28) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.RecyclerView" on path: DexPathList[[zip file "/data/app/com.example.goodresto-1/base.apk", zip file "/data/app/com.example.goodresto-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.goodresto-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.goodresto-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.goodresto-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.goodresto-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.goodresto-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.goodresto-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.goodresto-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.goodresto-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.goodresto-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.goodresto-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.goodresto-1/lib/x86, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at android.view.LayoutInflater.createView(LayoutInflater.java:583)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        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 androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.example.goodresto.MainMenu.onCreate(MainMenu.java:28) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
        Suppressed: java.lang.ClassNotFoundException: android.support.v7.widget.RecyclerView
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                ... 24 more

Gradle:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.support:cardview-v7:29.0.0'
    implementation 'com.android.support:recyclerview-v7:29.0.0'
}
安卓 android-gradle-plugin android-support-library androidx

评论

0赞 Niyas 8/18/2019
共享 XML 和活动文件
0赞 Ary Parker 8/18/2019
链接

答:

0赞 Gabriele Mariotti 8/18/2019 #1

这些依赖关系

implementation 'com.android.support:recyclerview-v7:29.0.0'
implementation 'com.android.support:cardview-v7:29.0.0'

不存在。

您可以在官方文档中查看修订历史

您可以:

  • 使用支持库的最新 28.0.0 版本
  • 迁移到 AndroidX

此外,您同时使用支持库和 androidx 库。你做不到。将所有依赖项迁移到 androidx。

0赞 Niyas 8/19/2019 #2

删除这些依赖项

implementation 'com.android.support:recyclerview-v7:29.0.0'
implementation 'com.android.support:cardview-v7:29.0.0'

并添加

implementation 'com.google.android.material:material:1.0.0'

并将 mainmenu.xml 改为

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".mainActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rvmainmenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

</RelativeLayout>