布尔变量总是等于 false

boolean variable always equals false

在我的应用程序中打开菜单时,它会根据游戏状态(暂停或未开始)显示一个额外的选项,但是按钮应该做什么的控制变量总是 returns 为 false。 我试图将 if 语句拆分为单独的语句并执行接受性能影响的处理,但在将鼠标悬停在 btnMenu 方法 onClick 中的 else if 语句上时,输出没有发生任何变化,它说:

Condition '!paused' is always 'true' when reached

相关代码如下:

Button btnStart;
Button btnMenu;
Button btnQuit;
Button btnShop;
Button btnSettings;
ImageView swipeDetector;
boolean menuToggle = false;
int gameRunning = 0;
boolean paused = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //calling object from activity_main.xml
    btnStart = (Button) findViewById(R.id.startButton);
    btnMenu = (Button) findViewById(R.id.menuButton);
    btnQuit = (Button) findViewById(R.id.quitBtn);
    btnShop = (Button) findViewById(R.id.shopButton);
    btnSettings = (Button) findViewById(R.id.settingsBtn);
    swipeDetector = (ImageView) findViewById(R.id.swiperReader);

    btnQuit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(gameRunning == 1) {
                gameRunning = 0;
                menuToggle = false;
                paused = false;
                //set game variables back to defaults
                btnQuit.setVisibility(View.GONE);
                btnSettings.setVisibility(View.GONE);
                btnShop.setVisibility(View.VISIBLE);
                btnStart.setVisibility(View.VISIBLE);
            }
        }
    });

    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            gameRunning = 1;
            menuToggle = false;
            paused = false;
            btnStart.setVisibility(View.GONE);
            btnShop.setVisibility(View.GONE);
        }
    });

    //broken, for some reason gameRunning always == 0
    btnMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (gameRunning == 1){
                paused = true;
            }
            if (!menuToggle && paused){
                btnStart.setVisibility(View.GONE);
                btnSettings.setVisibility(View.VISIBLE);
                swipeDetector.setVisibility(View.GONE);
                btnQuit.setVisibility(View.VISIBLE);
                menuToggle = true;

            //broken if statement
            }else if(!menuToggle && !paused){
                btnStart.setVisibility(View.GONE);
                btnSettings.setVisibility(View.VISIBLE);
                swipeDetector.setVisibility(View.GONE);
                menuToggle = true;

            } else {
                paused = false;
                btnSettings.setVisibility(View.GONE);
                btnQuit.setVisibility(View.GONE);
                btnStart.setVisibility(View.GONE);
                swipeDetector.setVisibility(View.VISIBLE);
                if(gameRunning == 0) {
                    btnStart.setVisibility(View.VISIBLE);
                }
                menuToggle = false;
            }
        }
    });

程序没有问题,因为我在大学时与一些朋友一起使用代码功能,当前版本的应用程序无法上传到虚拟设备,因此经过数小时尝试找到一些解决方案非常基本的代码,事实证明这是 Floobits 代码的问题。抱歉造成混淆。

第一条评论中提到的解决方案是,这是在说明我到达 else if 语句的时间,唯一可能的情况是该值将为真,而不是该值在整个代码中始终为真。

您正在声明一个必须为真的布尔条件,从 ifelse if 中删除 !menuToggle,它会变得更清楚。通常,当我遇到这些情况时,我会先花时间清理我的代码,这通常有助于我解决问题。这是清理的最后一个 onClick 方法的示例。

 public void onClick(View view) {

        if (!menuToggle) {
            btnStart.setVisibility(View.GONE);
            btnSettings.setVisibility(View.VISIBLE);
            swipeDetector.setVisibility(View.GONE);
            menuToggle = true;

            if(paused) {
                btnQuit.setVisibility(View.VISIBLE);
            }
        } else {

            btnSettings.setVisibility(View.GONE);
            btnQuit.setVisibility(View.GONE);
            btnStart.setVisibility(View.GONE);
            swipeDetector.setVisibility(View.VISIBLE);

            if(gameRunning == 0) {
                btnStart.setVisibility(View.VISIBLE);
            }
            paused = false;
            menuToggle = false;
        }
    }