简体   繁体   中英

Android Login Screen Won't Work Properly

So to begin with, I know I can easily get a source code for the login screen, and I may even do that. But the problem here is that I've been trying to figure out my mistake on that code for like 2 days, so I got "a little" frustrated. There it is:

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


//probably irrelevant after this point
public void startData(String email) {

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

This should work, but I haven't compiled it to test it.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM