Android KitKat: android.view.InflateException: Error inflating class android.support.design.widget.NavigationView

maohieng picture maohieng · Feb 18, 2016 · Viewed 7.7k times · Source

Below is my configuration. This works on Android Lollipop but crash on Android Kitkat.

I tried every workaround here but can't help:
Error inflating class android.support.design.widget.NavigationView

build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 23

    // Support RenderScript Library v8
    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true
}
}

ext{
    supportLibVersion = '23.1.1'
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile "com.android.support:appcompat-v7:${supportLibVersion}"
    compile "com.android.support:design:${supportLibVersion}"
    compile "com.android.support:cardview-v7:${supportLibVersion}"
    compile "com.android.support:recyclerview-v7:${supportLibVersion}"
}

Theme style.xml

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar" parent="AppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

Layout activity_main.xml

<android.support.v4.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">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.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_logged_out_kp"
        app:itemTextColor="@color/textColorPrimary"
        app:menu="@menu/activity_drawer" />

</android.support.v4.widget.DrawerLayout>  

Activity oncreate()

setSupportActionBar(toolbar);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

navigationView.setNavigationItemSelectedListener(this);
navHeaderView = navigationView.getHeaderView(0);  

Menu activity_drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_kpv"
            android:icon="@drawable/ic_menu_person"
            android:title="@string/action_kpv" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>  

Here is the error I get:

Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.NavigationView
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:627)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:703)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                                                                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                          at com.cammob.watershed.kp.ui.KPMainActivity.onCreate(KPMainActivity.java:68)
                                                                          at android.app.Activity.performCreate(Activity.java:5447)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:136) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5584) 
                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                                                                          at dalvik.system.NativeStart.main(Native Method) 
                                                                       Caused by: java.lang.reflect.InvocationTargetException
                                                                          at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                          at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:601)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:703) 
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:762) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:499) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                                                                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
                                                                          at com.cammob.watershed.kp.ui.KPMainActivity.onCreate(KPMainActivity.java:68) 
                                                                          at android.app.Activity.performCreate(Activity.java:5447) 
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) 
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:136) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5584) 
                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)  

Update: NavigationView's header nav_header_logged_out_kp.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="@dimen/nav_header_height"
             android:theme="@style/ThemeOverlay.AppCompat.Dark"

    >
    <!--android:background="@drawable/side_nav_bar"-->

    <ImageView
        android:id="@+id/imageBackground"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>

    <Button
        android:id="@+id/buttonLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/login"/>

</FrameLayout>

Answer

maohieng picture maohieng · Feb 18, 2016

I found it. The error comes from navigation's menu. When I created new project with navigation drawer, Android Studio generates complete work for me. There's a drawable folder named drawable-v21 which stores the icon xml files like this:

ic_menu_send.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z" />
</vector>  

And it's my bad. I missed to put ic_menu_send.png for pre-lollipop version.