代码之家  ›  专栏  ›  技术社区  ›  windoge_10

添加菜单项后Android Studio底部导航视图消失

  •  0
  • windoge_10  · 技术社区  · 7 年前

    我已经创建了菜单和底部导航视图及其菜单,但当我添加app:menu“@menu/Bottom\u Navigation\u menu”时,整个栏会消失,当我尝试运行它时,应用程序会崩溃。

    活动Main似乎已正确执行:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        tools:context="com.troy.connor.lightswitch.MainActivity">
    
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
    
        </FrameLayout>
    
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_nav_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimary"
            app:itemIconTint="@color/white"
            app:itemTextColor="@color/white"
            app:menu="@menu/bottom_navigation_menu"/>
    
    
    </LinearLayout>
    

    以及菜单项:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/buttonSlider"
            android:icon="@drawable/ic_slider"
            android:title="Sliders"
            />
        <item
            android:id="@+id/buttonPalette"
            android:icon="@drawable/ic_palette"
            android:title="Palette"
            />
        <item
            android:id="@+id/buttonShuffle"
            android:icon="@drawable/ic_shuffle"
            android:title="Shuffle"
            />
        <item
            android:id="@+id/buttonSchedule"
            android:icon="@drawable/ic_alarm_off"
            android:title="Schedule"
            />
        <item
            android:id="@+id/buttonConnect"
            android:icon="@drawable/ic_bt_off"
            android:title="Connect"
            />
        <item
            android:id="@+id/buttonStats"
            android:icon="@drawable/ic_timeline"
            android:title="Statistics"
            />
    </menu>
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Ben P.    7 年前

    如果检查堆栈跟踪以查找崩溃,您会发现:

    03-29 00:08:30.568  9850  9850 E AndroidRuntime:
        Caused by: java.lang.IllegalArgumentException:
        Maximum number of items supported by BottomNavigationView is 5.
        Limit can be checked with BottomNavigationView#getMaxItemCount()
    

    确实,如果你检查一下 the documentation for BottomNavigationView ,您将发现:

    当应用程序有三到五个顶级目标时,应该使用它。

    解决方案很简单:删除一个菜单项。如果这不是您的选择,那么您必须创建自己的底部导航实现,而不是使用 底部导航视图 .