MenuItem.setIcon throws NullPointerException when using BottomNavigationBar

253 Views Asked by At

I swapped out the drawer-navigation with a bottomnavigationbar and found out that I couldn't use actionviews any longer, which is why I used LayerDrawables for my icons.

final Menu menu = bottomNavigationView.getMenu();

notificationBullet = new NotificationDotDrawable(Color.Red, false, this);
final MenuItem nav = menu.findItem(R.id.nav_overview);
final LayerDrawable navMutate = (LayerDrawable) nav.getIcon().mutate();
navMutate.setDrawableByLayerId(R.id.ic_menu_upcoming_dot, notificationBullet);
nav.setIcon(navMutate);

Now I have the issue that the last method nav.setIcon(navMutate) throws a NullPointerException.

It's not that nav or navMutate is null, it's something further up in the OS, here's the stacktrace

I use both variables here:

final LayerDrawable navMutate = (LayerDrawable) nav.getIcon().mutate();

navMutate.setDrawableByLayerId(R.id.ic_menu_upcoming_dot, notificationBullet);

it CAN'T be because either of those are null, otherwise it would crash before that

java.lang.NullPointerException
    at android.graphics.drawable.LayerDrawable$LayerState.<init>(LayerDrawable.java:671)
    at android.graphics.drawable.LayerDrawable.createConstantState(LayerDrawable.java:107)
    at android.graphics.drawable.LayerDrawable.<init>(LayerDrawable.java:99)
    at android.graphics.drawable.LayerDrawable$LayerState.newDrawable(LayerDrawable.java:696)
    at android.support.design.internal.BottomNavigationItemView.setIcon(BottomNavigationItemView.java:224)
    at android.support.design.internal.BottomNavigationItemView.initialize(BottomNavigationItemView.java:100)
    at android.support.design.internal.BottomNavigationMenuView.updateMenuView(BottomNavigationMenuView.java:313)
    at android.support.design.internal.BottomNavigationPresenter.updateMenuView(BottomNavigationPresenter.java:64)
    at android.support.v7.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:291)
    at android.support.v7.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:1051)
    at android.support.v7.view.menu.MenuItemImpl.setIcon(MenuItemImpl.java:505)
    at _redacted_.ui.NavActivity.setupBottomNav(NavActivity.java:424)
    at _redacted_.ui.NavActivity.onCreate(NavActivity.java:182)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    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:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)

it only seems to happen on Android Versions below 5 (so SDK 16-19)

1

There are 1 best solutions below

0
On

You can set badge (notification) icon and count using native interface

just add material component dependency

implementation 'com.google.android.material:material:1.1.0-alpha09'

and use it like this:

private fun updateUnreadedMessagesCountView(count: Int) {
        if (count == 0)
            bnv_bottom_navigation.removeBadge(R.id.messagesListFragment)
        else
            bnv_bottom_navigation.getOrCreateBadge(R.id.messagesListFragment).number = count
    }

Where R.id.messagesListFragment is id of menu item. Tested on SDK 19