Having trouble creating an Android Login Page with

2019-08-23 10:14发布

问题:

I'm trying to create a generic login screen for Android. I'd like the program to check if the username and password (currently hardcoded) is listed. If the account is listed, you will be taken to a new screen called menuTest. I have the basic login working (without activity or intents). But when I try to add the intents, I get an error. How can I fix this? Below is my sample code.

Below are a few links containing screenshots of crash and logcat (edit: links are dead).

http://i.stack.imgur.com/ve7OL.png

http://i.stack.imgur.com/KLeQa.png

Thanks.


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Login extends Activity {
    // Declare our Views, so we can access them later
    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    private Button btnCancel;
    private TextView lblResult;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set Activity Layout
    setContentView(R.layout.login);

    // Get the EditText and Button References
    etUsername = (EditText)findViewById(R.id.username);
    etPassword = (EditText)findViewById(R.id.password);
    btnLogin = (Button)findViewById(R.id.login_enter);
    btnCancel = (Button)findViewById(R.id.login_cancel);
    lblResult = (TextView)findViewById(R.id.result);

    // Set Click Listener
    btnLogin.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Check Login
            String username = etUsername.getText().toString();
            String password = etPassword.getText().toString();

            if(username.equalsIgnoreCase("USER") && password.equals("132465")){
                //lblResult.setText("Login successful");
                Intent loginIntent = new Intent(this, menuTest.class);
                startActivity(loginIntent);
            }

            else {
                lblResult.setText("Login Failed");
            }
        }
    });


    btnCancel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Close the application
            finish();
        }
    });
}
}

回答1:

try changing Intent loginIntent = new Intent(this, menuTest.class); to Intent loginIntent = new Intent(Login.this, menuTest.class);



回答2:

these can be caused by two reasons

1) the destination activity (menuTest) may not be defined in manifest xml.

2) you may need to run inside UI thread e.g.

    Login.this.runOnUiThread(new Runnable() 
    public void run() {
        //lblResult.setText("Login successful");
        Intent loginIntent = new Intent(this, menuTest.class);
        startActivity(loginIntent);
        }
    });


回答3:

Ive used the setContetView function of the main activity this way you dont have to worry about the intents



回答4:

j1478 you asked "sorry to be a bother, but can you explain what the difference is in Login.this vs. this? – jl478 Apr 24 at 20:03"

It was answered up in the comments to your question.

"Because he is saying 'this' inside his button onClickListener the 'this' is referring to the onClickListener and not the activity – Thomas Owers Apr 24 at 21:11"

The onClickListener is an inner class, so this refers to the instance of that inner class. To get the Login instance you need to say Login.this, which you just so happen to have access too because inner classes inherit the scoped variables of their containing classes.