If instruction with boolean android

I've had a boolean problem with an if statement all day long and it is really starting to annoy me now! I've looked at other android themes here and the solutions just don't work.

My code started 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 different from other languages, and I only need one "=" sign, so I used that like:

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

      

It didn't work, and when I started searching online, after finding the previous thread, changed it to the following:

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

      

Now this doesn't look right to me, but hopes it works and it doesn't.

Since this is MainActivity, it is loaded first, but since I added all of this, the app will crash before it even loads when I choose the if statement how it works.

Any ideas?

+3


source to share


4 answers


I think you can just use

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

      



the way you followed is also correct, I didn't find any error other than that you are using an extra parenthesis in the state if(isSignedIn == false))

+3


source


it

if (isSignedIn == false)

      



is absolutely correct. (You can also write if (!isSignedIn)

, but that's just a matter of style.)

Note that since you never change the value isSignedIn

(at least not in the code you showed us), it always will false

.

+4


source


If the operators with booleans are the same as you do in Java ==

is the correct way to compare

The problem in your code is the extra parenthesis

if (isSignedIn == false)

)

blow>

+3


source


As a distraction from the question, but point out what your problem might be, your null pointer might be caused by you accessing a UI object that might not be ready to be set yet.

While some versions of the API do a great job of what you are doing, I found that many device / API commands are simply not ready to change anything other than the xml before onStart. The general guideline is to load data into onCreate, but don't start doing anything until onStart.

+2


source







All Articles