迁移到androidx后,Bottomnavigationview infalte错误

当我尝试启动我的应用程序时,我的Mainactivity在“ setContentView(R.layout.activity_main);”处的oncreate()方法中引发此错误. :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sanchez.worldgramproject/com.example.sanchez.worldgramproject.Fragments.MainActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.BottomNavigationView

迁移到androidx后出现此错误,经过进一步检查,我注意到Bottomnavigationview类中的import语句已过时(不是androidx格式,而是旧格式). Bottomnavigationview类文件是只读的,因此我无法对其进行编辑.

摇篮:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "com.example.sanchez.worldgramproject"
        minSdkVersion 21
        targetSdkVersion 28
        multiDexEnabled true
        versionCode 0
        versionName "0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
        }
    }
}



dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.github.madrapps:pikolo:1.1.6'
    implementation 'com.google.android.material:material:1.1.0-alpha02'
    implementation 'com.github.bumptech.glide:glide:3.8.0'
    implementation'com.firebaseui:firebase-ui-storage:2.3.0'
    implementation 'com.google.firebase:firebase-auth:16.1.0'
    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'com.jakewharton:butterknife:8.8.1'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.6'
    implementation 'de.hdodenhof:circleimageview:2.2.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.exifinterface:exifinterface:1.0.0'
    implementation 'com.google.firebase:firebase-storage:16.0.5'
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    implementation 'com.google.firebase:firebase-database:16.0.5'
    testImplementation 'junit:junit:4.12'

}


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

完整堆栈跟踪:https://pastebin.com/Yfek0YCH

有什么办法可以更改所有导入语句以适应新的androidx格式?我该如何在其他所有班级上做到这一点?

解决方法:

添加此依赖项:

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

并使用com.google.android.material.bottomnavigation.BottomNavigationView.

上一篇:如何设置要迁移到MaterialComponents,AndroidX和AppCompat的应用程序的样式


下一篇:android – Andoridx Room rx问题