【发布时间】:2018-03-07 12:12:06
【问题描述】:
enter image description here如您所见,我在导航抽屉中添加了 6 个项目,但在单击注销时不会触发它。抱歉,代码格式错误。我是新来的。
@Override public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_log_out) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.nav_profile:
break;
case R.id.nav_wallet:
break;
case R.id.nav_search:
break;
case R.id.nav_about_us:
break;
case R.id.nav_contact_us:
break;
case R.id.nav_log_out:
Intent intent = new Intent(this,LogIn.class);
FirebaseAuth.getInstance().signOut();
startActivity(intent);
}
这是我的菜单项 XML 文件。 id 与我在 XML 文件中声明的相同。在操作栏中注销工作正常,但在导航抽屉中不工作。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_profile"
android:icon="@drawable/ic_person_black_24dp"
android:title="Profile" />
<item
android:id="@+id/nav_wallet"
android:icon="@drawable/ic_attach_money_black_24dp"
android:title="EasyFare Wallet" />
<item
android:id="@+id/nav_search"
android:icon="@drawable/ic_directions_bus_black_24dp"
android:title="Search Bus" />
<item
android:id="@+id/nav_about_us"
android:icon="@drawable/ic_folder_shared_black_24dp"
android:title="About Us"/>
<item
android:id="@+id/nav_contact_us"
android:icon="@drawable/ic_email_black_24dp"
android:title="Contact Us" />
<item
android:id="@+id/nav_log_out"
android:icon="@drawable/ic_exit_to_app_black_24dp"
android:title="Log Out" />
</group>
</menu>
带有 DrawerLayout 的活动文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
-
您可以为此活动添加菜单 XML 文件吗?您可以在您的
R.id.nav_log_out案例中添加Log声明吗?我相当肯定 Zainab 的回答是正确的,但想排除另一种可能性。 -
您确定在代码中添加 nav_view.setNavigationItemSelectedListener(this) 吗?
-
@VinayakB 是的。
-
@Michael Dodd 我已经为活动添加了我的 XML 文件
-
请发布您的
Activity的布局,带有<DrawerLayout>的布局。