Home > Enterprise >  boolean variable always equals false
boolean variable always equals false

Time:05-09

When opening the menu in my application it shows an extra option depending on the state of the game (paused or not started) however the controlling variable for what the button should do always returns as false. I have attempted splitting the if statements into separate statements and performing the processing that was accepting the hit to performance but no changes happened to the outputs, upon hovering over the else if statement within the btnMenu method onClick it says:

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

related code below:

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;
            }
        }
    });

There was no problems with the program as i was using a code together feature with some friends at university the current version of the application would not upload to the virtual device and so after hours of trying to find a solution to some very basic code it turned out that it was an issue with Floobits code together. Sorry for the confusion.

the solution as mentioned in the first comment was that this was stating my the time the else if statement was reached the only possible scenario would be the value would be true not that the value is always true throughout the code.

CodePudding user response:

You are declaring a boolean condition that must be true, remove the !menuToggle from both the if and else if and it becomes more clear. Often, when I get into these situations I spend time cleaning up my code first and that often helps me sort out the issue. Here is an example of the last onClick method cleaned up.

 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;
        }
    }
  • Related