为什么 android studio 中的菜单项在点击时不起作用?

Why menu items in android studio don't work on click?

我正在 android 上开发一个项目,并且是全新的。我制作了一个带有一些菜单项的抽屉菜单。现在,当我想在菜单项中单击另一个 activity 时,抽屉会自动关闭,它不起作用。这里

private DrawerLayout mdrawerLayout;
private ActionBarDrawerToggle mdrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mdrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    mdrawerToggle = new ActionBarDrawerToggle(this, mdrawerLayout, R.string.open, R.string.close);
    mdrawerLayout.addDrawerListener(mdrawerToggle);

    mdrawerToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mdrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    int id = item.getItemId();
    if(id == R.id.setup){
        Intent csetup = new Intent(MainActivity.this, Subactivity.class);
        startActivity(csetup);

        return false;

    }
    return super.onOptionsItemSelected(item);
}

} `

这是因为你覆盖了错误的方法。您必须为抽屉覆盖 onNavigationItemSelected(),而不是为右上角设置菜单覆盖 onOptionsItemSelected()。参考以下代码:

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

    if (id == R.id.setup) {
             Intent csetup = new Intent(MainActivity.this, Subactivity.class);
             startActivity(csetup);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

首先使用如下所示的findViewById方法为您NavigationView找到视图-NavigationView navView=(NavigationView)findViewById(R.id.navView) 然后使用这个。

navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();

            if (id == R.id.setup) {
                Intent csetup = new Intent(MainActivity.this, Subactivity.class);
                startActivity(csetup);
            }

            return false;

        }
    });

然后它将起作用.. onOptionsItemSelected(MenuItem item) 用于访问 MenuItems 我们使用 onCreateOptionsMenu() 方法在工具栏或其他布局上膨胀。

试试这个: 在您的 onCreate 方法中添加以下代码:

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

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

在您的主要活动中实施 lister class:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener

然后在你的 onNavigationItemSelected 方法中看起来像这样:

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

        if (id == R.id.setup) {
        //call new activity
         }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
     }

希望对您有所帮助