使用BottomNavigationBar时,MenuItem.setIcon会抛出NullPointerException

时间:2017-12-21 07:36:30

标签: java android nullpointerexception bottomnavigationview layerdrawable

我用底部导航栏换出抽屉导航,发现我再也不能使用操作视图,这就是为什么我将LayerDrawable用于我的图标。

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);

现在我遇到的问题是最后一个方法nav.setIcon(navMutate)抛出了NullPointerException。

不是navnavMutate为空,它在操作系统中是进一步的,这里是堆栈跟踪

我在这里使用两个变量:

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

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

它可能因为其中任何一个为空,否则它会在此之前崩溃

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)

它似乎只发生在低于5的Android版本(因此SDK 16-19)

1 个答案:

答案 0 :(得分:0)

您可以设置徽标(通知)图标并使用本机界面计数

只需添加材料成分依赖性

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

并像这样使用它:

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
    }

R.id.messagesListFragment是菜单项的ID。 在SDK 19上测试