我用底部导航栏替换了抽屉导航,发现我不能再使用动作视图,这就是为什么我使用 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 的问题。
不是那个nav
或navMutate
为空,它在操作系统中更进一步,这是堆栈跟踪
我在这里使用这两个变量:
最后的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)