迁移到AndroidX后,膨胀com.google.android.material.navigation.NavigationView类时出错

时间:2019-07-08 08:49:53

标签: android androidx android-navigationview

在我将项目更新为AndroidX并将targetSdkVersion设置为28之后,我的项目在从商店安装进行Beta测试时崩溃了。

以下是我收到的错误日志:

2019-07-08 08:28:33.026 32011-32011/? E/AndroidRuntime: FATAL EXCEPTION: main Process: hu.itq.oakprotection, PID: 32011
        java.lang.RuntimeException: Unable to start activity ComponentInfo{hu.itq.oakprotection/hu.itq.oakprotection.MainActivity}: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.google.android.material.navigation.NavigationView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
            at android.app.ActivityThread.-wrap12(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
            at android.os.Handler.dispatchMessage(Handler.java:108)
            at android.os.Looper.loop(Looper.java:166)
            at android.app.ActivityThread.main(ActivityThread.java:7425)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
         Caused by: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.google.android.material.navigation.NavigationView
         Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.google.android.material.navigation.NavigationView
         Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
            at android.view.LayoutInflater.createView(LayoutInflater.java:658)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:801)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
            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.c(Unknown Source:23)
            at androidx.appcompat.app.AppCompatActivity.setContentView(Unknown Source:4)
            at hu.itq.oakprotection.MainActivity.onCreate(Unknown Source:6)
            at android.app.Activity.performCreate(Activity.java:7372)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
            at android.app.ActivityThread.-wrap12(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
            at android.os.Handler.dispatchMessage(Handler.java:108)
            at android.os.Looper.loop(Looper.java:166)
            at android.app.ActivityThread.main(ActivityThread.java:7425)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
         Caused by: java.lang.NoSuchMethodError: No interface method d()I in class Lorg/xmlpull/v1/XmlPullParser; or its super classes (declaration of 'org.xmlpull.v1.XmlPullParser' appears in /system/framework/core-libart.jar)
            at androidx.appcompat.view.SupportMenuInflater.a(Unknown Source:5)
            at androidx.appcompat.view.SupportMenuInflater.inflate(Unknown Source:25)
            at com.google.android.material.navigation.NavigationView.b(Unknown Source:12)
            at com.google.android.material.navigation.NavigationView.<init>(Unknown Source:255)
            at com.google.android.material.navigation.NavigationView.<init>(Unknown Source:2)
            at java.lang.reflect.Constructor.newInstance0(Native Method) 
            at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 

以下是我在build.gradle文件中的依赖项:

android {
    compileSdkVersion 28
    def versionMajor = 1
    def versionMinor = 0
    def versionPatch = 1
    def versionBuild = 4 // ezt kell átírni új verzió kirakásakor

    defaultConfig {
        applicationId "hu.itq.oakprotection"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
        versionName "${versionMajor}.${versionMinor}.${versionPatch}"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation 'androidx.core:core:1.0.2'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.annotation:annotation:1.1.0'

    implementation 'com.payumoney.sdkui:plug-n-play:1.2.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.android.material:material:1.1.0-alpha07'
    implementation 'androidx.browser:browser:1.0.0'
    implementation 'com.google.android.gms:play-services-base:17.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //Test error miatt kellett
    implementation 'androidx.annotation:annotation:1.1.0'

    //Aszinkron hálózatokhoz
    implementation 'com.android.volley:volley:1.1.1'

    //Facebook
    implementation 'com.facebook.android:facebook-android-sdk:4.34.0'
    //Osm Droid
    implementation 'org.osmdroid:osmdroid-android:6.1.0'
    implementation 'com.github.MKergall:osmbonuspack:6.4'

    implementation 'com.github.angads25:filepicker:1.1.1'
    //Tile provider igényli
    implementation 'mil.nga.geopackage:geopackage-android:3.0.2'
    implementation 'com.j256.ormlite:ormlite-android:5.1'
    implementation 'mil.nga.geopackage:geopackage-core:3.0.2'
    implementation 'ar.com.hjg:pngj:2.1.0'
    implementation 'mil.nga:tiff:2.0.0'
    implementation('mil.nga.geopackage:geopackage-android:3.0.2') {
        exclude group: 'com.google.android.gms', module: 'play-services'
        exclude group: 'com.google.maps.android', module: 'android-maps-utils'
        exclude group: 'com.android.support', module: 'support-v13'
    }
    //Glide
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
    //Mapsforge rendering and database support, which is LGPL
    implementation 'org.mapsforge:mapsforge-map-android:0.8.0'
    implementation 'org.mapsforge:mapsforge-map:0.8.0'
    implementation 'org.mapsforge:mapsforge-themes:0.8.0'
    // ExoPlayer
    implementation 'com.google.android.exoplayer:exoplayer:2.7.3'
    //Youtube
    implementation 'com.github.HaarigerHarald:android-youtubeExtractor:v1.7.0'}

activity_main.xml:

<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    tools:context=".MainActivity">

    <include
        layout="@layout/app_bar_main" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        app:itemIconTint="@color/colorPrimary"
        app:itemTextColor="@color/colorPrimary"
        android:backgroundTint="@color/colorWhite"/>

</androidx.drawerlayout.widget.DrawerLayout>

为什么我收到android.view.InflateException的{​​{1}}错误?

9 个答案:

答案 0 :(得分:3)

在我的情况下,问题是清单文件中的主题值

<activity
        android:name=".ui.BarcodeReaderDrawerActivity"
        android:label="@string/title_activity_barcode_reader_drawer">
        android:theme="@style/AppBaseTheme.NoActionBar"
    </activity>

更改

<activity
        android:name=".ui.BarcodeReaderDrawerActivity"
        android:label="@string/title_activity_barcode_reader_drawer"
        android:theme="@style/Theme.MaterialComponents.Light.NoActionBar">
    </activity>

答案 1 :(得分:1)

有多个原因可以导致此错误发生。检查以下内容并确保它们正确:

  • 如果您使用的是默认导航抽屉模板,请检查您的代码中是否有drawable中使用过的colorcom.google.android.material.navigation.NavigationView中使用的activity_main.xml资源来自Android Studio的代码。
  • 检查可绘制文件是否位于res/drawable文件夹中,而不是res/drawable-v21中。
  • 检查您在android:backgroundTint()内部是否使用过android:srcNavigationView或类似的东西。由于它们在android API级别21以下无法正常工作,因此请改用app:backgroundTintapp:srcCompat

答案 2 :(得分:1)

就我而言,问题出在Navigation.NavigationView app:headerLayout 布局中 我正在设置

<ImageView 
  app:srcCompat="@mipmap/ic_launcher_round"
/> 

问题在于 ic_launcher_round ,因为它位于文件夹 mipmap-anydpi-v26 中 我正在使用API​​级别为25的Android版本7.1.1

希望这对任何人都有帮助

答案 3 :(得分:0)

android.view.InflateException:表示您的XML文件本身有问题。

请检查所有尺寸,颜色资源以及在XML标记中分配的其他资源。

或在此处发布您的XML文件。

答案 4 :(得分:0)

您可以检查XML文件。 xml中出现问题,例如菜单,导航标题和其他错误。就我而言,在res / menu / menu.xml

中出错

答案 5 :(得分:0)

对于那些选择的答案没有帮助的人,我发现NavigationView主题存在问题,它应该是appCompat主题(至少对我而言)。 创建新的样式后:

<style name="NavView" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    ...
</style>

并在我的mainactivity.xml中的NavigationView中将其与android:theme =“ @ style / NavView”一起使用,该应用现在可以完美运行。

答案 6 :(得分:0)

就我而言,我手动将res/drawable-v24中的所有内容移至res/drawable文件夹中,并将res/mipmap-anydpi-v26移至res/mipmap-hdpi,并在装有Android 5和6的手机中解决了问题。

答案 7 :(得分:0)

我在升级到 Androidx 时遇到了这个错误,很难找到。问题出在活动主题上。

在styles.xml中,不知何故我有错误的android材料主题,导致导航栏没有附加到操作栏。

from:

    <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">

to:

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">

答案 8 :(得分:-1)

就我而言,在导航标题布局中,我的图像太大且不适合屏幕。我更改了那个图标,它运行了。