java Android Studio菜单在每次项目选择时打开所有项目

zkure5ic  于 2022-11-20  发布在  Java
关注(0)|答案(1)|浏览(144)

我目前正在开发一个Android Studio项目,我遇到了一个问题,无论我在菜单中选择哪个选项,它都会连续将活动更改为所有选项,然后崩溃。以下是我的主页菜单功能:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    menu.removeItem(R.id.HomePage);
    if (sp.getLong("User_Id", -1) != -1) {
        menu.removeItem(R.id.Login);
        menu.removeItem(R.id.SignUp);
    }
    else {
        menu.removeItem(R.id.LogOut);
    }
    if (sp.contains("User_Id")){
        if (!database.userDao().GetUserById(sp.getLong("User_Id", -1)).getUserEmail().equals(R.string.admin_email))
            menu.removeItem(R.id.UserList);
    }
    else
        menu.removeItem(R.id.UserList);
    return true;
}

@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.SignUp: {
            startActivity(new Intent(MainActivity.this, RegisterActivity.class));
        }
        case R.id.Login: {
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
        }
        case R.id.UserList: {
            startActivity(new Intent(MainActivity.this, UserActivity.class));
        }
        case R.id.LogOut:{
            builder.setMessage(R.string.logout_dialog_message).setTitle(R.string.logout_dialog_title).setCancelable(true).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    editor.clear();
                    editor.commit();
                    finish();
                    overridePendingTransition(0, 0);
                    startActivity(getIntent());
                    overridePendingTransition(0, 0);
                }
            }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
        case R.id.About: {
            Dialog dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.custom_about_dialog);
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.setCancelable(true);
            dialog.getWindow().getAttributes().windowAnimations = R.style.animation;
            ImageView image = dialog.findViewById(R.id.IVLogo);
            TextView text = dialog.findViewById(R.id.TVText);
            image.setBackground(getDrawable(R.drawable.logo));
            text.setText("About us: We are regel2, the new best second hand app. In this app, you are able to buy and sell any product that you want, for any price that you want, all inside 1 app.");
            dialog.show();
        }
    }
    return super.onOptionsItemSelected(item);
}

我试着限制更多的选项,或者把开关切换成一系列的“如果“,但是没有用。
注意:它甚至会打开已删除的菜单项(Aka menu.removeItem(___)),当我单击“关于”项时,它不会发生,因为它是一个对话框,所以可能会有不同的行为。

8ehkhllq

8ehkhllq1#

你需要把

break;

作为每个case块的最后一条语句,否则执行将继续到最后。

相关问题