0

In my app the activity is crashing when the it is trying to read some text from an editText and displaying it onto another activity.

Here is the code that is getting the text from the editText:

Intent intent = new Intent(this, CurrentHomework.class);
       EditText editText = (EditText) findViewById(R.id.homeDue);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);

Here is the code that is getting the text from the string and putting it on an textView:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String message = intent.getStringExtra(AddNewHomework.EXTRA_MESSAGE);
    TextView textView = (TextView) findViewById(R.id.textView99);
    textView.setText(message);
party58965
  • 41
  • 1
  • 5

2 Answers2

0

You missed setContentView().

Set your layout in your activity first prior to using it.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   setContentView(your_layout_id);

    Intent intent = getIntent();
    String message = intent.getStringExtra(AddNewHomework.EXTRA_MESSAGE);
    TextView textView = (TextView) findViewById(R.id.textView99);
    textView.setText(message);
}
Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
0

To clarify Shadab's answer, also ensure you prefix the layout ID with R.layout, so that it reads:

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

    Intent intent = getIntent();
    String message = intent.getStringExtra(AddNewHomework.EXTRA_MESSAGE);
    TextView textView = (TextView) findViewById(R.id.textView99);
    textView.setText(message);
}
mohsin
  • 36
  • 3