Menuitem.Seticon使用底部navigationbar时会抛出NullPoInterException



我用底部绘制键换了抽屉 - 奖励,发现我不能再使用action视图,这就是为什么我将layerDrawables用于图标的原因。

>
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是无效的,而是OS中进一步的东西,这是stacktrace

我在这里使用两个变量:

最终layerdrawable navmutate =(layerDrawable) nav .getIcon()。mutate();

navmutate .setDrawableBylayerId(r.id.ic.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(SDK 16-19)以下的Android版本上发生

您可以设置徽章(通知)图标,并使用本机接口进行计数

只需添加材料组件依赖项

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

上测试

相关内容

  • 没有找到相关文章

最新更新