我正在尝试使我的应用程序正常工作以打开我的活动以单击导航抽屉,但我无法获取菜单ID。
.java
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.home_id:
Toast.makeText(MainActivity.this, "tarun", Toast.LENGTH_SHORT).show();
Intent home_Intent = new Intent(MainActivity.this,MainActivity.class);
startActivity(home_Intent);
break;
return true;
}
});
这是我的菜单项xml,这里还提供了ID和名称,它们将在导航视图中显示给我
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/home_id"
android:title="Home"
android:icon="@drawable/home"/>
</group>
</menu>