简体   繁体   中英

App crash when using EditText for app written in Android Studio

I'm having a problem when trying to get data from "EditText" in Android. The app crashes immediately when I try to assign "EditText" value to a String variable (I used toString() method).

The problem is in the two lines after the onCreate method.

Here is my code:

public class MainActivity extends AppCompatActivity {

EditText em;
EditText pw;
private FirebaseAuth mAuth;

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

    EditText em = (EditText) findViewById(R.id.email1);
    EditText pw = (EditText) findViewById(R.id.password1);

    mAuth = FirebaseAuth.getInstance();
}

// When I remove those two lines the app runs normally
String email = em.getText().toString(); 
String password = pw.getText().toString();



void login(View v){
    mAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        FirebaseUser user = mAuth.getCurrentUser();
                        String Logged = user.getEmail().toString();

                        Intent intent = new Intent(MainActivity.this, afterLogin.class);
                        intent.putExtra("email", Logged);
                        startActivity(intent);
                        finish();

                    } else {
                        // If sign in fails, display a message to the user.

                        Toast.makeText(MainActivity.this, "Wrong email/password.", Toast.LENGTH_SHORT).show();

                    }

                }
            });
}

}

Why are these statements outside?

String email = em.getText().toString(); 
String password = pw.getText().toString();  

You are trying to assign these before onCreate was executed. Extracting data at class level is similar to doing this in constructor. Think about it.. constructor runs first, then the onCreate executes.

So, you are essentially running

null.getText().toString(); //because em and pw were still null

Instead of doing this, please move these statements to login method or any other event method you might want to create.

You're declaring and initializing String email and String password before onCreate so the EditText s are null. Move the String initialization into your onCreate like this:

EditText em;
EditText pw;
String email;
String password;

onCreate {
  EditText em = findViewById(R.id.email1);
  EditText pw = findViewById(R.id.password1);

  email = em.getText().toString;
  password = pw.getText().toString;
}

Of course, the EditText s will probably be empty at that point, so you'll probably want to get their values later, or use a TextWatcher .

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