将listner添加到导航抽屉,菜单项不起作用

时间:2017-03-03 09:08:02

标签: java android

我正在尝试将listner添加到我的导航抽屉菜单项但它不起作用请检查我的脚本并检查此图像[https://www.dropbox.com/s/imzufywwj5qyjlj/Nav_Menu.png?dl=0]谢谢。

ProfileActivity.java



NavigationView navigationView =(NavigationView)findViewById(R.id.nav_view);

        View headerView = navigationView.getHeaderView(0);
        TextView prousername = (TextView) headerView.findViewById(R.id.pro_username);
        TextView promail = (TextView) headerView.findViewById(R.id.pro_mail);
        TextView prophone = (TextView) headerView.findViewById(R.id.pro_phone);

        ProDrawareLayout = (DrawerLayout)findViewById(R.id.activity_profile);
        ProToggle = new ActionBarDrawerToggle(this,ProDrawareLayout, R.string.open, R.string.close);

        ProDrawareLayout.addDrawerListener(ProToggle);
        ProToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        prousername.setText(SharedPrefManager.getInstance(this).getUsername());
        promail.setText(SharedPrefManager.getInstance(this).getUserEmail());
        prophone.setText(SharedPrefManager.getInstance(this).getUserPhone());

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Switch(item.getItemId()){
                    case R.id.nav_logout:
                        SharedPrefManager.getInstance(this).logOut();
                        finish();
                        startActivity(new Intent(this, MainActivity.class));
                }




Navigation_menu.xml



<group android:checkableBehavior="single">
        <item android:id="@+id/nav_account"
            android:title="My account"/>

        <item android:id="@+id/nav_settings"
            android:title="Settings"/>

        <item android:id="@+id/nav_logout"
            android:title="Log Out"/>
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/Rate_id"
                android:title="@string/rate_us"
                android:showAsAction="never"></item>
            <item
                android:id="@+id/Contact_id"
                android:title="@string/contact_us"
                android:showAsAction="never"></item>
            <item
                android:id="@+id/About_id"
                android:title="@string/about_us"
                android:showAsAction="never"></item>
        </menu>
    </item>
&#13;
&#13;
&#13;

activity_profile.xml

&#13;
&#13;
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start"
        android:background="@color/colorBG"
        app:headerLayout="@layout/navigation_header"
        android:id="@+id/nav_view" >

    </android.support.design.widget.NavigationView>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

取下开关盒功能的大写字母。

switch代替Switch

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Switch(item.getItemId()){  // the issue is here
                    case R.id.nav_logout:
                        SharedPrefManager.getInstance(this).logOut();
                        finish();
                        startActivity(new Intent(this, MainActivity.class));
                }

编辑:

您不能使用this作为对您的活动的引用,因为您的OnNavigationItemSelectedListener就属于这种情况。

请尝试改为:

创建一个类字段private Context mContextmContext函数上的onCreate mContext = thismContext; 使用navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { Switch(item.getItemId()){ // the issue is here case R.id.nav_logout: SharedPrefManager.getInstance(mContext).logOut(); finish(); startActivity(new Intent(mContext, MainActivity.class)); }

android:layout_below="@+id/bottom_navigation"