如何在导航抽屉中添加 activity

How to add activity in Navigation Drawer

我正在尝试在我的导航抽屉中添加一个 activity。我已经在我的主要 activity in switch case 中放入了带有项目 id 的意图。我的应用程序运行完美但 intent 没有工作,当我单击我添加了 intent 抽屉的项目时关闭但我无法移动到 activity,下面是代码。

我打算注册

public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
NavigationView navigationView;
androidx.appcompat.widget.Toolbar toolbar;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);
    textView = findViewById(R.id.heyTv);
    drawerLayout = findViewById(R.id.drawer_layout);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = findViewById(R.id.navView);


    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.home:
                    Toast.makeText(MainActivity.this, "Trial", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.profile:
                    Toast.makeText(MainActivity.this, "User", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.logout:
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("Logout")
                            .setIcon(R.drawable.ic_logout)
                            .setMessage("Sure! You want to logout")
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(MainActivity.this, SigninActivity.class);
                                    startActivity(intent);
                                }
                            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).setNeutralButton("Help", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "Press yes to logout", Toast.LENGTH_SHORT).show();
                        }
                    }).show();
                    break;
                case R.id.signup:
                    Intent intent = new Intent(MainActivity.this, SignupActivity.class);

                    break;
                case R.id.share:
                    Toast.makeText(MainActivity.this, "Share", Toast.LENGTH_SHORT).show();
                case R.id.mail:
                    Toast.makeText(MainActivity.this, "Contact us", Toast.LENGTH_SHORT).show();
            }
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;
        }
    });
}

private void startActivities(Intent intent) {
    startActivities(intent);

}

@Override
public void onBackPressed() {
    if (drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }
    else {
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("Exit")
                .setIcon(R.drawable.ic_logout)
                .setMessage("Sure! You want to exit?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finishAffinity();
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setNeutralButton("Other", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "Press yes to exit the app", Toast.LENGTH_SHORT).show();
            }
        }).show();
    }
}

您忘记启动 activity。创建意图后添加一行

Intent intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(intent);