底部导航视图getChildAt()错误

时间:2017-10-08 15:33:12

标签: android bottomnavigationview

我在项目中一起使用了navigationview和bottomnavigationview,但getChildAt()出错。

这是我的Main2Activity。

    setContentView(R.layout.activity_main2);
        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavView_Bar);

    BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
    Menu menu = bottomNavigationView.getMenu();
    MenuItem menuItem = menu.getItem(0);
    menuItem.setChecked(true);

    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){

                case R.id.ic_android:
                    Intent intent0 = new Intent(Main2Activity.this, Kategori.class);
                    startActivity(intent0);
                    break;

                case R.id.ic_books:
                    Intent intent2 = new Intent(Main2Activity.this, Kategori.class);
                    startActivity(intent2);
                    break;
            }
            return false;
        }
    });

由于navigationview,我使用activity_main2作为布局。实际上,底部导航是content_main。

这是logcat:

 java.lang.RuntimeException: Unable to start activity 
ComponentInfo{info.androidhive.firebasenotifications/info.androidhive.firebasenotifications.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.BottomNavigationView.getChildAt(int)' on a null object reference
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 
...
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.BottomNavigationView.getChildAt(int)' on a null object reference
                                                                                          at info.androidhive.firebasenotifications.BottomNavigationViewHelper.disableShiftMode(BottomNavigationViewHelper.java:13)
                                                                                          at info.androidhive.firebasenotifications.Main2Activity.onCreate(Main2Activity.java:108)

这是bottomnavigationviewhelper

 public static void disableShiftMode(BottomNavigationView view) {
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
    try {
        Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
        shiftingMode.setAccessible(true);
        shiftingMode.setBoolean(menuView, false);
        shiftingMode.setAccessible(false);
        for (int i = 0; i < menuView.getChildCount(); i++) {
            BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
            //noinspection RestrictedApi
            item.setShiftingMode(false);
            // set once again checked value, so view will be updated
            //noinspection RestrictedApi
            item.setChecked(item.getItemData().isChecked());
        }
    } catch (NoSuchFieldException e) {
        Log.e("BNVHelper", "Unable to get shift mode field", e);
    } catch (IllegalAccessException e) {
        Log.e("BNVHelper", "Unable to change value of shift mode", e);
    }

0 个答案:

没有答案