How do I change icons of the menu items in navigation drawer in android studio?

I’m trying to change the icons on the menu of navigation drawer activity. The MainActivity.java is the same as the default one. The icons eg: medbox,medicine,appointment,doctor,me,settings are .png files I placed in Drawable-v21 folder. The project is getting built but I’m getting the following runtime exceptions:

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

Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView

Caused by: java.lang.reflect.InvocationTargetException

Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f020056 a=3 r=0x7f020056}

I’ve tried almost all solutions given on stack overflow but I couldn’t get rid of this. This is activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">

    <item
        android:id="@+id/medbox"
        android:icon="@drawable/medbox"
        android:title="@string/pillbox" />
    <item
        android:id="@+id/medicine"
        android:icon="@drawable/medicine"
        android:title="@string/medications" />
    <item
        android:id="@+id/appointment"
        android:icon="@drawable/appointment"
        android:title="@string/appointments" />
    <item
        android:id="@+id/doctor"
        android:icon="@drawable/doctor"
        android:title="@string/doctors" />
</group>

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/me"
            android:icon="@drawable/me"
            android:title="@string/me" />
        <item
            android:id="@+id/settings"
            android:icon="@drawable/settings"
            android:title="@string/settings" />
    </menu>
</item>

</menu>

This is drawables.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<item name="medbox" type="drawable">@drawable/medbox</item>
<item name="medicine" type="drawable">@drawable/medicine</item>
<item name="appointment" type="drawable">@drawable/appointment</item>
<item name="doctor" type="drawable">@drawable/doctor</item>
<item name="me" type="drawable">@drawable/me</item>
<item name="settings" type="drawable">@drawable/settings</item>

</resources>

Edit: layout with nav_view

<?xml version="1.0" encoding="utf-8"?>

<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_main"
    app:menu="@menu/activity_main_drawer" />

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

styles.xml:

<resources>

<!-- 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">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" 
parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" 
parent="ThemeOverlay.AppCompat.Light" />

</resources>

Entire stacktrace:

Process: com.example.shradha.mediday, PID: 30514

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shradha.mediday/com.example.shradha.mediday.MainAct    ivity}: android.view.InflateException: Binary XML file line #16: Error   inflating class android.support.design.widget.NavigationView
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                             at android.os.Looper.loop(Looper.java:193)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                             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:824)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.NavigationView
                                                                             at android.view.LayoutInflater.createView(LayoutInflater.java:620)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                             at com.example.shradha.mediday.MainActivity.onCreate(MainActivity.java:24)
                                                                             at android.app.Activity.performCreate(Activity.java:5264)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                             at android.os.Looper.loop(Looper.java:193) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                             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:824) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                                             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:594)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                             at com.example.shradha.mediday.MainActivity.onCreate(MainActivity.java:24) 
                                                                             at android.app.Activity.performCreate(Activity.java:5264) 
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302) 
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                             at android.os.Looper.loop(Looper.java:193) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                             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:824) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                                             at dalvik.system.NativeStart.main(Native Method) 
                                                                          Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020057
                                                                             at android.content.res.Resources.getValue(Resources.java:1133)
                                                                             at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:321)
                                                                             at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
                                                                             at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
                                                                             at android.support.v7.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:421)
                                                                             at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.prepareMenuItems(NavigationMenuPresenter.java:495)
                                                                             at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.update(NavigationMenuPresenter.java:436)
                                                                             at android.support.design.internal.NavigationMenuPresenter.updateMenuView(NavigationMenuPresenter.java:112)
                                                                             at android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:245)
                                                                             at android.support.design.widget.NavigationView.<init>(NavigationView.java:169)
                                                                             at android.support.design.widget.NavigationView.<init>(NavigationView.java:95)
                                                                             at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                             at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
                                                                             at android.view.LayoutInflater.createView(LayoutInflater.java:594) 
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                             at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                                                                             at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                             at com.example.shradha.mediday.MainActivity.onCreate(MainActivity.java:24) 
                                                                             at android.app.Activity.performCreate(Activity.java:5264) 
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302) 
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                             at android.os.Looper.loop(Looper.java:193) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                             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:824) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                                             at dalvik.system.NativeStart.main(Native Method) 

Answer

The actual problem was that you placed the png inres/drawable-21 so pre-lollipop devices can’t find it, hence the android.content.res.Resources$NotFoundException.

So copy the png files from res/drawable-21 to res/drawable.

Leave a Reply

Your email address will not be published. Required fields are marked *