0

In my Android app, when I try to use this code:

EditText et = (EditText) findViewById(R.id.editText);
String s = et.getText().toString();

my app crashes. This event is triggered from a dialog positive click.

How can I access the text of an EditText without my app crashing?

qbush
  • 712
  • 2
  • 9
  • 28
  • 2
    what is the error message? – SalmonKiller Jan 02 '15 at 00:43
  • There is no error message. The app just crashes. – qbush Jan 02 '15 at 00:45
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – stkent Jan 02 '15 at 00:55
  • THe data you are setting to the String variable if null, it would crash the application. Check the value of editText before setting it for null. It might resolve your issue. – Prem Jan 02 '15 at 00:55
  • See the bottom part of [this answer](http://stackoverflow.com/questions/18861082/alert-dialog-positive-button-issue/18861110#18861110) that is likely your issue. But it's impossible to say for sure without the stacktrace. – codeMagic Jan 02 '15 at 01:13
  • are you calling the "et" definition before setContentView? – Trebia Project. Jan 02 '15 at 01:39
  • I am using the 'et' definition in a function that is triggered my my dialog's onPositiveClick. – qbush Jan 02 '15 at 17:29

1 Answers1

2

There might be a couple of problems:

  1. There is no such element as editText

  2. EditText class is not imported.

  3. The EditText element is empty, so the String s is null.

Here are a few ways to find the actual problem:

  1. Use an IDE like Android Studio. It reports many small problems right then and there.

  2. Review your code carefully and make sure it accounts for any possible situations.

  3. Set up System.out.print statements throughout to better log the variable values and other properties.

SalmonKiller
  • 2,183
  • 4
  • 27
  • 56