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