2016-06-19 61 views
1

我使用導航活動從一個活動移動到另一個活動。我想將子菜單添加到NavigationView <的菜單中,但無法找到在子菜單中查找標識項的方法。Android - 在SubMenu中查找項目的ID

<menu>  
<item android:title="Communicate"> 
    <menu> 
     <item android:id="@+id/about_us" android:icon="@android:drawable/ic_menu_share" 
      android:title="About us" /> 
    </menu> 
</item></menu> 

 

@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    switch (id) { 
     case R.id.nav_order: { 
      break; 
     } 
     case R.id.nav_search: { 
      break; 
     } 
    } 

    return true; 
} 

我想補充:case R.id.about_us。 但Android Studio無法找到此ID。 我怎麼能做到這一點,我做錯了什麼?

+0

post'menu.xml' full file code here。 – Ironman

回答

2

您的代碼爲我工作的罰款。您的菜單中缺少一件東西:

<menu xmlns:android="http://schemas.android.com/apk/res/android"/> <!--the xmlns is missing --> 
</menu> 
+1

感謝您的好回答:) –

0

嘗試寫這樣的:

The drawer itesm below have been classified into groups 

    <group 
     android:id="@+id/group_1" 
     android:checkableBehavior="single"> 
     <item 
      android:id="@+id/navigation_item_1" 
      android:icon="@drawable/ic_number_0" 
      android:title="@string/navigation_item_1" /> 
     <item 
      android:id="@+id/navigation_item_2" 
      android:icon="@drawable/ic_number_1" 
      android:title="@string/navigation_item_2" /> 
    </group> 
    <group 
     android:id="@+id/group_2" 
     android:checkableBehavior="single"> 
     <item 
      android:id="@+id/navigation_item_3" 
      android:icon="@drawable/ic_number_2" 
      android:title="@string/navigation_item_3" /> 
     <item 
      android:id="@+id/navigation_item_4" 
      android:icon="@drawable/ic_number_3" 
      android:title="@string/navigation_item_4" /> 
    </group> 
    <group 
     android:id="@+id/group_3" 
     android:checkableBehavior="single"> 
     <item 
      android:id="@+id/navigation_item_5" 
      android:icon="@drawable/ic_number_4" 
      android:title="@string/navigation_item_5" /> 
    </group> 

    OR 
    The drawer items below have been categorized according to headers 
    <item 
     android:id="@+id/navigation_item_1" 
     android:icon="@drawable/ic_number_0" 
     android:title="@string/navigation_item_1"> 
     <menu> 
      <item 
       android:id="@+id/navigation_item_2" 
       android:icon="@drawable/ic_number_1" 
       android:title="@string/navigation_item_2" /> 
      <item 
       android:id="@+id/navigation_item_3" 
       android:icon="@drawable/ic_number_2" 
       android:title="@string/navigation_item_3" /> 
     </menu> 
    </item> 

    <item 
     android:id="@+id/navigation_item_4" 
     android:icon="@drawable/ic_number_3" 
     android:title="@string/navigation_item_4"> 
     <menu> 
      <item 
       android:id="@+id/navigation_item_5" 
       android:icon="@drawable/ic_number_4" 
       android:title="@string/navigation_item_5" /> 
     </menu> 

</item>