-4

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

                    }

                }
            });
}

}

2
  • You can't write code outside of a method like you have tried to do.
    – takendarkk
    Commented Jan 22, 2019 at 17:01
  • Please don't put tags in the title of your question. Also, please read the tag usage guidance carefully before posting. Commented Jan 22, 2019 at 17:22

2 Answers 2

0

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.

0
0

You're declaring and initializing String email and String password before onCreate so the EditTexts 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 EditTexts will probably be empty at that point, so you'll probably want to get their values later, or use a TextWatcher.

1
  • You're welcome. If my answer solved you problem please mark my answer as the solution!
    – alex87
    Commented Jan 22, 2019 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.