Error inflating class android.support.design.widget.BottomNavigationView 出现错误,无法加载android.support.design.widget.BottomNavigationView类。

3
Caused by: android.view.InflateException: Binary XML file line #19: Binary XML file line #19: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class android.support.design.widget.BottomNavigationView

Gradle源码

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0-alpha01'
    implementation 'androidx.cardview:cardview:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

布局XML源代码

 <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="parent"
        app:menu="@menu/menu_bottomnavigationview">  
 </android.support.design.widget.BottomNavigationView>

我错过了什么吗? Android Studio是否仍支持 android.support.design.widget.BottomNavigationView ?


我认为你应该检查@menu/menu_bottomnavigationview菜单。要检查它,你可以尝试删除app:menu="@menu/menu_bottomnavigationview"(如果可以)。然后重新运行应用程序。 - John Le
@thrillingchase - 听起来问题是由于“android.support”与“androidx” Gradle依赖项和/或Java包的缺失/冲突依赖关系引起的。也许你只需要在XML布局中更改包名就可以解决问题了。问题是否已经“解决”? - paulsm4
5个回答

3

1
好的发现:我没有注意到“androidx”依赖! - paulsm4

1
尝试
implementation 'com.google.android.material:material:1.1.0-alpha10' 

并且

<com.google.android.material.bottomnavigation.BottomNavigationView
...
>
</com.google.android.material.bottomnavigation.BottomNavigationView>

0

你正在使用 androidx 库。

因此,你必须使用

implementation 'com.google.android.material:material:1.2.0-alpha01'

这对我有用。


0
在我的情况下,我在菜单上添加了超过5个项目,导致了崩溃。只有在菜单中添加了5个项目后,我才停止看到这个崩溃。

0

根据文档, 它是:

  • 版本 26.1.0 中新增的

  • Maven artifact com.android.support:design:27.1.0 所属

不幸的是,这份文档是针对(旧版)android.support 库的。这个主题讨论如何在 (新版) androidx 库中使用 BottomNavigationView:

如何设置 Jetpack 导航与 material.BottomNavigationView

正如 Gabriele Mariotti 和 RavenYang 所说,如果您正在使用 androidx,则需要:

  • 在 build.gradle 中指定 com.google.android.material:material:1.1.0-alpha10

    ... **AND ** ...

  • 在布局 XML 中指定 androidx 包 com.google.android.material.bottomnavigation.BottomNavigationView

示例(android.support):

dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:27.1.1'
   implementation 'com.android.support:design:27.1.1'
   ...

示例(androidx):

dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'androidx.appcompat:appcompat:1.0.2'
   implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
   implementation 'com.google.android.material:material:1.1.0-alpha10' 
   ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接