Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Onur Çevik
Onur Çevik
2,390 Points

Android Login Screen Won't Work Properly

Hi everyone,

I'm trying to integrate a login screen to my application, and put a single condition which lets only a specific e-mail and password to login to the app. Problem is probably in if-else conditions of onCreate method. If works well, I can login using that specific e-mail and password, yet when I enter an invalid information, application freezes and crashes after a point. I don't even get the error message I wrote. Note that I already tried to use boolean x = false/true for the while loop, still didn't work. Would really appreciate any help on that. Thanks in advance.

public class LoginActivity extends ActionBarActivity {

public Button loginButton;
private EditText emailText;
private EditText passwordText;
private TextView errorView;
private String x = "go";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    loginButton = (Button) findViewById(R.id.loginButton);

    errorView = (TextView) findViewById(R.id.errorView);

    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            while (x.equals("go")){
            emailText = (EditText) findViewById(R.id.emailText);
            passwordText = (EditText) findViewById(R.id.passwordText);

            if (emailText.getText().toString().equals("meric.erler@metu.edu.tr")
                    && passwordText.getText().toString().equals("meric1234")){
                    x="return";
            }
            else{
                errorView.setText("Invalid E-mail or Password.");
            }
                x.equals("go");
            }
            String email = emailText.getText().toString();
            startData(email);}
    });
}
    public void startData(String email){

        Intent intent = new Intent(LoginActivity.this, DataActivity.class);
        intent.putExtra("email", email);
        startActivity(intent);
}}

1 Answer

Dai Phong
Dai Phong
20,395 Points

The while (x.equals("go")) will loop over an over and never stop when you enter invalid information. You could not use while loop, just use if-else like this:

loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            emailText = (EditText) findViewById(R.id.emailText);
            passwordText = (EditText) findViewById(R.id.passwordText);

            if (emailText.getText().toString().equals("meric.erler@metu.edu.tr")
                    && passwordText.getText().toString().equals("meric1234")){
                String email = emailText.getText().toString();
                startData(email);}
            }
            else{
                errorView.setText("Invalid E-mail or Password.");
            } 
    });

And ensure that LoginActivity.this does not try to connect to the internet in the main thread.

Onur Çevik
Onur Çevik
2,390 Points

Thanks, that worked. But what do you mean by the final sentence? How do I prevent that, and where exactly is the problem?

Dai Phong
Dai Phong
20,395 Points

I mean if you try to connect to the internet in LoginActivity you should not do it in the main thread, in a task instead. I don't know what you do in LoginActivity, so I just guest that. Never mind if you do not make any internet request in LoginActivity.

Onur Çevik
Onur Çevik
2,390 Points

Oh I got it. Thanks for the heads up :)