0

I am really new to android. And my English speaking is not good. So,I was going to get a username using an EditText and set it to a TextView But the problem is that the EditText is in the first class (MyActivity),and the TextView is in the second class(MyAcyivity2). I did everything such as FindViewById and.... but when I set on a click listener :

Textview1.setText(EditText1.getText())

and open the app, by clicking on button it says: Unfortunately app has stopped.

what to do?

Shalev Shalit
  • 1,945
  • 4
  • 24
  • 34
  • 2
    you need to send value of EditText to MyActivity2 by put extra, like this: http://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data – Sajad Garshasbi Jul 23 '15 at 20:37
  • Pay attention to your app debug or logcat, it provides you usefull information such what caused the problem you are facing. See the answer for how to fix it. – Murillo Ferreira Jul 23 '15 at 20:54

1 Answers1

5

In MyActivity :

EditText et = (EditText) findViewById(R.id.editText1);

On button click, inside onClickListener :

void onClick(View view) {
    Intent intent = new Intent(MyActivity.this, MyActivity2.class);
    intent.putExtra("myString", et.getText().toString());
    startActivity(intent);

}

In MyActivity2 inside onCreate()

String myString = getIntent().getStringExtra("myString");
TextView tv = findViewById(R.id.textView1); 
tv.setText(myString);
Samrat Dutta
  • 1,727
  • 1
  • 11
  • 23