【问题标题】:Navigation Drawer click not triggering导航抽屉点击未触发
【发布时间】: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 的布局,带有&lt;DrawerLayout&gt; 的布局。

标签: android navigation-drawer


【解决方案1】:

您必须在注销时为大小写添加括号..

case R.id.nav_log_out:
{
        Intent intent = new Intent(this,LogIn.class);
        FirebaseAuth.getInstance().signOut();
        startActivity(intent);
}

【讨论】:

  • 正确答案但错误的理由。 OP 需要括号,因为他声明了一个对象的新实例(在这种情况下为 Intent)。只要没有声明任何新内容,多行 switch 语句就可以不用括号。
  • @ZainabJamil 这对我来说不起作用。感谢您的帮助。
  • @AyushBherwani 我会说它首先没有达到那种情况。请发布您的菜单文件的 XML 和比较 id 与 R.id.nav_log_out 的值的日志语句
  • 您可能已经在您的选项菜单 xml 文件中为您的注销选项提供了一些其他 ID。请检查一下。 :)
  • 您确定要从导航视图中注销还是使用操作栏菜单注销? @AyushBherwani
【解决方案2】:

您能否将代码包含在您的活动的 onCreate 中?

一种可能是您忘记将您的活动注册为导航视图的侦听器。

mNavigationView.setNavigationItemSelectedListener(this)

【讨论】:

    【解决方案3】:

    点击其他项目,比如说第二个,会触发这个项目吗?

    如果是,请在您的注销案例中添加一个中断语句并创建一个默认案例。

    您也可以尝试用以下示例替换您的代码。

    @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);
                break;
                }
            DrawerLayout drawer;
            drawer = findViewById(R.id.THE_ID_OF_YOUR_DRAWER);  
            drawer.closeDrawer(GravityCompat.START);
            return true;
       }
    
    }
    

    还要确保在您的 OnCreate 、 OnStart 或 onResume 中包含以下内容

     DrawerLayout drawer;
    
     drawer = findViewById(R.id.THE_ID_OF_YOUR_DRAWER);
    

    【讨论】:

    • 点击其他项目不会触发注销案例。
    • 我的意思是如果点击nav_wallet会触发nav_wallet等等。最可能的解释是您的标签有问题,正如其他用户所指出的那样。
    • 我在 onCreate 中有 DrawerLayout 抽屉 = findViewById(R.id.drawer_layout)。
    • 您的课程是否扩展了AppCompatActivity 并实现了NavigationView.OnNavigationItemSelectedListener?
    • 是的,我的班级实现了NavigationView.OnNavigationItemSelectedListener
    【解决方案4】:

    我将为您提供我用于我的项目的代码,并尝试查看您是否发现任何差异可以解释您的代码不起作用的事实。

    AnnouncementsActivity.java

        .
        .
        .
        .
    
    
    
    public class AnnouncementsActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {
    
    
    
    
    
    
        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
    
                setContentView(R.layout.activity_announcements);
                .
                .
                .
                .
    
    
    
                    Toolbar toolbar = findViewById(R.id.toolbar);
                    setSupportActionBar(toolbar);
    
    
    
                    DrawerLayout drawer;
    
                    drawer = findViewById(R.id.drawer_layout_astiko);
    
    
    
                    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                    drawer.addDrawerListener(toggle);
                    toggle.syncState();
    
                    toolbar.setNavigationIcon(R.mipmap.ic_toolbar);
    
    
    
                    NavigationView navigationView =  findViewById(R.id.nav_view);
                    navigationView.setNavigationItemSelectedListener(this);
    
               .
               .
               .
               .
    
    
    
               }
    
    
    
    
    
    
            @SuppressWarnings("StatementWithEmptyBody")
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    
                int id = item.getItemId();
    
                if (id == R.id.nav_first_item) {
    
                    if(prev_activity_routes){
                        Intent intent = new Intent(this,RoutesActivity.class);
                        intent.putExtra("Direction Forth",bus_direction_forth);
                        intent.putExtra("Team",team);
                        intent.putExtra("Route name", value1);
                        intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
                        startActivity(intent);
                    }else{
    
                        if(isAstiko){
                            Intent intent = new Intent(this,TicketIssuanceActivityAstiko.class);
                            intent.putExtra("Direction Forth",bus_direction_forth);
                            intent.putExtra("Team",team);
                            intent.putExtra("Route name", value1);
                            intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
                            startActivity(intent);
                        }else{
                            Intent intent = new Intent(this,TicketIssuanceActivity.class);
                            intent.putExtra("Direction Forth",bus_direction_forth);
                            intent.putExtra("Team",team);
                            intent.putExtra("Route name", value1);
                            intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
                            startActivity(intent);
                        }
    
                    }
    
                } else if (id == R.id.nav_second_item) {
                    Intent intent = new Intent(this,DefinedTicketsActivity.class);
                    intent.putExtra("Direction Forth",bus_direction_forth);
                    intent.putExtra("Team",team);
                    intent.putExtra("Route name", value1);
                    intent.putExtra("odigos_onomateponimo",odigos_onomateponimo);
                    intent.putExtra("PreviousActivityRoutes",prev_activity_routes);
                    intent.putExtra("isAstiko",isAstiko);
                    startActivity(intent);
                }
    
                .
                .
                .
                .
    
    
                }else if (id == R.id.nav_eighth_item) {
                    Intent intent = new Intent(this, SignInActivity.class);
                    startActivity(intent);
                }
    
                DrawerLayout drawer;         
                drawer = findViewById(R.id.drawer_layout);        
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        }
    
    
    
    
    
    }
    

    ActivityAnnouncements.xml

    <?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_announcements"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="500dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:theme="@style/NavigationDrawerStyle"
            app:headerLayout="@layout/my_nav_header"
            app:menu="@menu/my_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
    

    app_bar_announcements.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.e_analysis.AlphaPro.MobileTicketing.AnnouncementsActivity">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/MyTheme">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="65dp"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/MyTheme"/>
    
        </android.support.design.widget.AppBarLayout>
    
        <include layout="@layout/content_announcements" />
    
    
    </android.support.design.widget.CoordinatorLayout>
    

    content_announcements.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="-0dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:orientation="vertical"
        android:background="@color/colorPrimary"
        android:gravity="fill"
        tools:context="com.e_analysis.AlphaPro.MobileTicketing.AnnouncementsActivity"
        tools:showIn="@layout/app_bar_announcements">
    
    
        .
        .
        .
        .
    
    
    
    </LinearLayout>
    

    my_drawer.xml 在菜单子文件夹中

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
    
        <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_first_item"
                    android:icon="@mipmap/ic_printer_black"
                    android:title="@string/nav_first_item_value"/>
                <item
                    android:id="@+id/nav_second_item"
                    android:icon="@mipmap/ic_tune_vertical"
                    android:title="@string/nav_second_item_value" />
    
                .
                .
                .
                .
    
                <item
                    android:id="@+id/nav_eighth_item"
                    android:icon="@mipmap/ic_logout"
                    android:title="@string/nav_eighth_item_value" />
            </group>
    
    
    </menu>
    

    my_nav_header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">
    
        .
        .
        .
        .
    
    </LinearLayout>
    

    这段代码可以正常工作(当然不是这样),所以试着用它做一些交叉检查,看看你缺少什么。当您发现时,很可能会很容易忽略和令人沮丧。祝你好运!

    【讨论】:

    • @AyushBherwani 你有没有运气解决你的问题朋友?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2013-07-04
    • 2014-04-02
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多