【问题标题】:How to solve Error inflating class android.support.design.widget.BottomNavigationView?如何解决错误膨胀类android.support.design.widget.BottomNavigationView?
【发布时间】:2019-09-07 17:46:43
【问题描述】:

我正在尝试实现底部导航视图的方法,但是当我运行我的代码时出现此错误

原因:android.view.InflateException:二进制 XML 文件第 19 行:二进制 XML 文件第 19 行:类 android.support.design.widget.BottomNavigationView 膨胀错误 引起:android.view.InflateException:二进制 XML 文件第 19 行:错误膨胀类 android.support.design.widget.BottomNavigationView 原因:java.lang.ClassNotFoundException:在路径上找不到类“android.support.design.widget.BottomNavigationView”:DexPathList[

我应该怎么做才能解决这个问题?

activity_home 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    app:layout_behavior="android:.support.design.widget.BottomSheetBehaviour"
    tools:context=".HomeActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_navigation">

    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/purpleBae"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/home_menu" />

</RelativeLayout>

build.gradle(模块应用)

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.gerobokgoapp"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        // Exclude file to avoid
        // Error: Duplicate files during packaging of APK
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }

    compileOptions {
        incremental true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }


}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


    //add libraries
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
    implementation 'com.google.firebase:firebase-firestore:21.0.0'
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    implementation 'com.facebook.android:account-kit-sdk:4.39.0'

    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    testImplementation 'androidx.test.ext:junit:1.1.1'
}

【问题讨论】:

    标签: java android android-layout bottomnavigationview


    【解决方案1】:

    你不能在同一个项目中同时使用 support 和 androidx 库,除非你使用 Jetpack 工具,否则肯定会发生冲突。将您的支持库迁移到 androidx

    Androidx 布局文件中的引用也应该都是 androidx 工件,而不是其他。

    因此标签如下:

      <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/purpleBae"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/home_menu" />
    

    应该是:

    <com.google.android.material.bottomnavigation.BottomNavigationView
      android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/purpleBae"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/home_menu" />
    

    【讨论】:

    • 相同的答案,请不要复制我的答案@Nidheesh
    • 您只提到了库实现。不是底部导航视图的使用。 gradile 文件中已经存在实现。
    【解决方案2】:

    添加以下依赖项:

    dependencies {
        // https://mvnrepository.com/artifact/com.google.android.material/material
        implementation "com.google.android.material:material:1.0.0"
    }
    

    改用com.google.android.material.bottomnavigation.BottomNavigationView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多