0

In this app, I want to test how to send data between activities. This is the code that I used:

public class MainActivity extends AppCompatActivity {
    EditText parola,email;
    Button buton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        parola.findViewById(R.id.enterPassword);
        email.findViewById(R.id.enterMail);
        buton.findViewById(R.id.button);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String StrEmail=email.getText().toString();
        String StrParola=parola.getText().toString();
        buton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(getApplicationContext(),ActivityB.class);
                i.putExtra("mail",StrEmail);
                i.putExtra("pass",StrParola);
                startActivity(i);
            }
        });
    }
}

But every time I open the app, it just crashes.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
George
  • 21
  • 1
  • you should get the editText input texts after the user clicks the button, initially, those are empty. put them inside onClick. paste your error log also. – Amir Dora. Nov 16 '20 at 10:17
  • Does this answer your question? [How do I get extra data from intent on Android?](https://stackoverflow.com/questions/4233873/how-do-i-get-extra-data-from-intent-on-android) – Amir Dora. Nov 16 '20 at 10:18
  • there seems to be an error in findViewById()..... it should be parola=findViewById(R.id.enterPassword); change similarly for email and button. – Amir Dora. Nov 16 '20 at 10:19

1 Answers1

1

You forgot to add = before intiliasing the Editext and Button

public class MainActivity extends AppCompatActivity {
EditText parola,email;
Button buton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    parola = findViewById(R.id.enterPassword); // forgot =
    email = findViewById(R.id.enterMail);
    buton = findViewById(R.id.button);

    String StrEmail=email.getText().toString();
    String StrParola=parola.getText().toString();
    buton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(getApplicationContext(),ActivityB.class);
            i.putExtra("mail",StrEmail);
            i.putExtra("pass",StrParola);
            startActivity(i);
        }
    });
}
}
HARSH ASHRA
  • 176
  • 1
  • 4
  • 20
  • 2
    He was trying to initialize them using a dot ```email.findBiewById()```. Also he was doing it BEFORE the setContentView(). – private static Nov 16 '20 at 10:15