【问题标题】:Can't inflate RecycleView无法为 RecycleView 充气
【发布时间】:2018-07-31 20:24:20
【问题描述】:

我已将 RecycleView 添加到我的 XML 中,如下所示:

    <androidx.recyclerview
        android:id="@+id/recycleViewTest"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

我在 build.gradle 中的依赖项是这样定义的:

dependencies
{
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.google.android.material:material:1.0.0-alpha1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation "androidx.recyclerview:recyclerview:1.0.0-alpha1"
}

项目可以编译,但是在我运行它时它会崩溃,并且出现以下异常:

Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class androidx.recyclerview
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class androidx.recyclerview
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.recyclerview" on path: DexPathList[[zip file "/data/app/

我的 compileSdkVersion 设置为 28。请问有什么建议吗?

更新:

我添加了依赖:

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

所以我的依赖部分现在看起来像这样:

    dependencies
    {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation 'com.google.android.material:material:1.0.0-alpha1'
        implementation 'com.android.support.constraint:constraint-layout:1.1.0'
        implementation 'com.android.support:recyclerview-v7:28.0.0-beta01'
    }

但现在我遇到了一个新错误:

Manifest merger failed : Attribute application@appComponentFactory value=(androidx.core.app.CoreComponentFactory) from [androidx.core:core:1.0.0-alpha1] AndroidManifest.xml:22:18-86
is also present at [com.android.support:support-compat:28.0.0-beta01] AndroidManifest.xml:22:18-91 value=(android.support.v4.app.CoreComponentFactory).
Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:6:5-21:19 to override.

所以我添加了以下属性:

tools:replace="android:appComponentFactory"

application 标签如下:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    tools:replace="android:appComponentFactory"
    android:theme="@style/AppTheme">

但现在我又遇到了一个错误:

Manifest merger failed with multiple errors, see logs

【问题讨论】:

  • xml 标签应该是androidx.recyclerview.widget.RecyclerView

标签: java inflate-exception androidx


【解决方案1】:

androidx.recyclerview 是一个包名,你需要一个 View 的包+类名来通过 XML 膨胀它,我找过你,但包 androidx.recyclerview 现在有视图。

如果您打算使用 RecyclerView (https://developer.android.com/guide/topics/ui/layout/recyclerview)

正确的gradle语句是:

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

XML 标记为:

android.support.v7.widget.RecyclerView

【讨论】:

  • 谢谢,稍后再试。奇怪的是,当我在 xml 中将“androidx.recyclerview”更改为“androidx.recyclerview.widget.RecyclerView”时,它起作用了
  • 我所说的androidx.recyclerview 是一个包,您需要查看全名。只是我注意到您尝试使用的库仍然是 alpha 并且还没有准备好投入生产。尝试使用旧的支持,直到它变得稳定。
  • 清除,谢谢!我添加了您建议的库,但出现新错误,请您检查一下吗?我更新了我的问题
  • 你必须删除另一个“androidx”
  • 它已经被删除了,我用我目前拥有的依赖列表更新了我的问题,谢谢!
【解决方案2】:

如果切换到AndroidX,则需要更改

  1. android.useAndroidX = trueandroid.enableJetifier = true 在 gradle.properties 中
  2. build.gradle 中的依赖项
  3. Java 中的导入
  4. Java 中的类名和布局 XML 文件

始终如一地适用于之前确实属于支持库的所有部分。 您不能将支持库与 androidx 混合使用,否则 Manifest 合并会像您的情况一样抱怨。

要特别切换 RecycleView,请更改

  • implementation 'androidx.recyclerview:recyclerview:1.0.0'的依赖
  • XML 布局文件标记为androidx.recyclerview.widget.RecyclerView

映射可以在“迁移到 AndroidX”指南here 中找到。

【讨论】:

  • 谢谢。这应该被接受,因为它正确地使用了 AndroidX 而不是 appcompat-v7。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多