Material-components-android: [NavigationView] App crashes when using NavigationView in alpha8

Created on 18 Jul 2019  路  5Comments  路  Source: material-components/material-components-android

Description: App crashes when inflating xml containing NavigationView. 'Error inflating class com.google.android.material.navigation.NavigationView'

Expected behavior: No crash, unless I'm doing something wrong that should be done differently.

Source code:

<?xml version="1.0" encoding="utf-8"?>
<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:context=".views.activities.MainActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

    </LinearLayout>

    <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"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu">

    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>
class MainActivity : androidx.core.app.AppCompatActivity() {
     .
     .
     .

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)      <- crashes here
        .
        .
        .
    }
}



md5-7a21cfd67e8c17fd0f143b01e03fa816



E/AndroidRuntime: FATAL EXCEPTION: main
    Process: shio.at.jdrop, PID: 5345
    java.lang.RuntimeException: Unable to start activity ComponentInfo{shio.at.jdrop/shio.at.jdrop.views.activities.MainActivity}: android.view.InflateException: Binary XML file line #33: Binary XML file line #33: Error inflating class com.google.android.material.navigation.NavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.view.InflateException: Binary XML file line #33: Binary XML file line #33: Error inflating class com.google.android.material.navigation.NavigationView
     Caused by: android.view.InflateException: Binary XML file line #33: 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:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        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.setContentView(AppCompatDelegateImpl.java:545)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at shio.at.jdrop.views.activities.MainActivity.onCreate(MainActivity.kt:40)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.BootstrapMethodError: Exception from call site #16 bootstrap method
        at com.google.android.material.internal.ScrimInsetsFrameLayout.<init>(ScrimInsetsFrameLayout.java:67)
        at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:116)
        at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:112)
        at java.lang.reflect.Constructor.newInstance0(Native Method)聽
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)聽
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)聽
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)聽
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)聽
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)聽
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)聽
        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.setContentView(AppCompatDelegateImpl.java:545)聽
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)聽
        at shio.at.jdrop.views.activities.MainActivity.onCreate(MainActivity.kt:40)聽
        at android.app.Activity.performCreate(Activity.java:7136)聽
        at android.app.Activity.performCreate(Activity.java:7127)聽
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)聽
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)聽
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)聽
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)聽
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)聽
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)聽
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)聽
        at android.os.Handler.dispatchMessage(Handler.java:106)聽
        at android.os.Looper.loop(Looper.java:193)聽
        at android.app.ActivityThread.main(ActivityThread.java:6669)聽
        at java.lang.reflect.Method.invoke(Native Method)聽
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)聽
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)聽
E/AndroidRuntime: Caused by: java.lang.ClassCastException: Bootstrap method returned null
        ... 31 more

Android API version: 28

Material Library version: 1.1.0-alpha08 (works with alpha07)

Device: Emulator

bug

Most helpful comment

I'm facing a similar issue as well, but with BottomNavigationView.

I believe this can be fixed by setting the source/target compatibility Java version to 1.8 as the library updated the source/target compatibility version to 1.8 in 1.1.0-alpha08 (although one of the devs has commented that this will be reverted in 1.1.0-alpha09 - see #426):

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

Note: The commit that updated the required Java version was 3280f6f (which was included in 1.1.0-alpha08) and the commit that will revert the required Java version to 1.7 is a628bf5.

Hope this helps!

All 5 comments

This is probably down to api version. The latest changelog sais it now depends on android 'Q' sdk, while I'm using 28 still. I'm gonna keep using 07 for the time being and try this again at the weekend. Don't really have that great of an internet until then to be downloading preview versions of android studio and the new sdk.


We don't have to depend on all those things mentioned in the changelog in our project do we? Since, right now the libraries I depend on in my project are these

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.11.1"
    implementation 'org.jetbrains.anko:anko-commons:0.10.8'
    implementation 'org.jetbrains.anko:anko-sqlite:0.10.8'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1"

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'com.google.android.material:material:1.1.0-alpha07'

So the only library mentioned in the changelog I depend on would be androidx.appcompat:appcompat. I tried updating that to the version mentioned in the changelog, but that didn't help. As I said I'll keep it like this for the time being.

I now updated to API level 29 using android studio 3.5 RC1. Does not resolve the issue.

I'm facing a similar issue as well, but with BottomNavigationView.

I believe this can be fixed by setting the source/target compatibility Java version to 1.8 as the library updated the source/target compatibility version to 1.8 in 1.1.0-alpha08 (although one of the devs has commented that this will be reverted in 1.1.0-alpha09 - see #426):

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

Note: The commit that updated the required Java version was 3280f6f (which was included in 1.1.0-alpha08) and the commit that will revert the required Java version to 1.7 is a628bf5.

Hope this helps!

Strange I was under the impression kotlin already uses java 1.8 by default. And I was already using java 8 features such as default methods for interfaces and lamda expressions. But I'm not sure what part of that was java and what part of that was kotlin magic... It works when setting the java version to 1.8.

It works down to Android 8 and since that's the lowest Android version I'm planning to support (for now anyways). I'm just gonna keep it at 1.8, don't see a reason to use 1.7 instead. NOTE that I'm not saying it does not work below Android 8, I simply didn't even try that.

Thank's for the help! :)

The bug you're facing has since been fixed in 1.1.0-alpha09 (released 2 days ago), which reverts the compiled Java version from 8 to 7.

Was this page helpful?
0 / 5 - 0 ratings