onNavigationItemSelected(@NonNull MenuItem menuItem)未触发,请在跳过之前检查代码

时间:2019-05-26 21:39:17

标签: java android navigation-drawer drawerlayout android-navigation-drawer

onNavigationItemSelected(@NonNull MenuItem menuItem)未触发,请先跳过检查,以免认为这是一个相同的问题,但这确实是一个问题,我可以正常进行其他活动,但不能进行此活动。

MainActivity.java

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private ActionBarDrawerToggle t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setNavigation();
}
    private void setNavigation() {
    DrawerLayout dl = findViewById(R.id.mdrawerLayout);
    t = new ActionBarDrawerToggle(this, dl,R.string.Open, 
    R.string.Close);

    dl.addDrawerListener(t);
    t.syncState();




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

}

    private void setupDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(MenuForNearBy.this);
}
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch(menuItem.getItemId())
        {
        case R.id.homeBtn:
            Toast.makeText(MenuForNearBy.this, "Home",Toast.LENGTH_SHORT).show();
            menuItem.setChecked(true);

            finish();
            startActivity(new Intent(MenuForNearBy.this,AlarmLocationActivity.class));
            break;
        case R.id.nearByPlacesBtn:
            Toast.makeText(MenuForNearBy.this, "Near By Places",Toast.LENGTH_SHORT).show();
            menuItem.setChecked(true);

            break;
        case R.id.logOutBtn:
            Toast.makeText(MenuForNearBy.this, "Logging Out",Toast.LENGTH_SHORT).show();
            menuItem.setChecked(true);
            finish();
            break;
        default:
            return true;
    }
    return true;
}

0 个答案:

没有答案