If statement with booleans android

24,109

Solution 1

i think you can simply use

if(!isSignedIn)
{
  btnSignIn.setText("SignIn");
}
else
{       
  btnSignIn.setText(message);
}        

the way you followed is also correct i didn't find any mistake in except you are using extra bracket in condition if(isSignedIn == false))

Solution 2

This

if (isSignedIn == false)

is perfectly correct. (You could also write if (!isSignedIn), but that's just a matter of style.)

Note that, since you never change the value of isSignedIn (at least not in the code you have shown us), it will always be false.

Solution 3

If statements with boolean are same how you do it in Java, == is the right way to compare

The problem in your code is extra bracket

if (isSignedIn == false))

Solution 4

Just to deviate from the question, but point out what is possibly your problem, your null pointer could be because you are accessing a UI object that may well not be ready to have it's text set yet.

While some API versions cope fine with what you're doing, I've found many device/API combos simply aren't ready to have anything changed from what's in the xml until onStart. The general guidance is to load data in onCreate, but not start doing anything until onStart.

Share:
24,109
Defterniko
Author by

Defterniko

Updated on July 09, 2022

Comments

  • Defterniko
    Defterniko almost 2 years

    I have been having a problem with a Boolean in an if statement all day and it is really starting to irritate me now!! I have looked at other Android threads on here and the solutions just don't seem t work.

    My code started off like this:

    public class MainActivity extends Activity 
    {
    public static boolean isSignedIn = false;       
    
    public final static String USERNAME_MESSAGE = "com.example.libnoise.MESSAGE";
    Button btnSignIn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);       
    
    
        btnSignIn = (Button) findViewById(R.id.btnSignIn);
        Intent intent = getIntent();
        String message = intent.getStringExtra(PlayZone.USERNAME_MESSAGE); 
    
        if(isSignedIn == false))
        {
       btnSignIn.setText("SignIn");
        }
        else
        {       
             btnSignIn.setText(message);
        }        
    }
    

    Then I had a thought that made it's not like other languages and I only need one "=" sign so I had it as this:

        if(isSignedIn = false)
        {
       btnSignIn.setText("SignIn");
        }
        else
        {       
             btnSignIn.setText(message);
        }  
    

    That didn't work and that's when I started looking online, after finding a previous thread on here changed it to the following:

        if("false".equals(isSignedIn))
        {
       btnSignIn.setText("SignIn");
        }
        else
        {       
             btnSignIn.setText(message);
        }  
    

    Now that doesn't look right to me in the first place but hoped it would work and it didn't.

    As this is the MainActivity it loads first however since I added all this, the app crashes before it will even load when I take out the if statement it work as expected.

    Any ideas?