-1

I am making this try-out game and after someone guesses a number, the EditText view, needs to be cleared.

Here is my onCreate:

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

    Button b = (Button) findViewById(R.id.check);
    final EditText input = (EditText) findViewById(R.id.number);
    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (!input.getText().toString().matches("")) {
                input.setText("");
                handleGuess(Integer.parseInt(input.getText().toString()));
            } else {
                Toast.makeText(getApplicationContext(), "Je hebt geen nummer ingevuld!", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    });

    input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                if (!input.getText().toString().matches("")) {
                    input.setText("");
                    handleGuess(Integer.parseInt(input.getText().toString()));
                } else {
                    Toast.makeText(getApplicationContext(), "Je hebt geen nummer ingevuld!", Toast.LENGTH_SHORT)
                            .show();
                }
            }
            return false;
        }
    });
}

Whenever I try to press Enter on my softkeyboard or press the button, it just says "RaadHetGetal stopped."

Any ideas? If you need to see any more, just say it.

Amarjit
  • 4,327
  • 2
  • 34
  • 51
Guus Huizen
  • 165
  • 1
  • 2
  • 9
  • Check out [Unfortunately MyApp has stopped](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). – nhaarman Jul 28 '15 at 10:11
  • 3
    if your application crashes always post the log :) – ezcoding Jul 28 '15 at 10:11
  • Before calling any action for input edittext you need to check if input.getText()!=null or input.getText.toString().trim().length()!=0 then perform actions only otherwise it will give NullPointerException. – Ricky Khatri Jul 28 '15 at 10:14
  • i'm not getting why u using `input.setText("");` ? – Anand Singh Jul 28 '15 at 10:25
  • Nevermind guys, I found out. I put the setText(""); in front of the handleGuess method, what made it give an NPE since there was nothing in the EditText view.. I am so stupid. Thanks for your help! – Guus Huizen Jul 28 '15 at 10:26
  • @GuusHuizen If your problem was solved, you should accept an answer so people see this no longer needs attention. – milez Jul 28 '15 at 10:29

2 Answers2

0

In your both listeners you have at least this error:

You set the text to nothing

input.setText("");

And try to parse an int from it directly after. This results in NumberFormatException

handleGuess(Integer.parseInt(input.getText().toString()));

So if you want to clear the text after handleGuess, you can call clear().

handleGuess(Integer.parseInt(input.getText().toString()));
input.clear();
milez
  • 2,201
  • 12
  • 31
0

Right, you obtain a java.lang.NumberFormatException, at line:

Integer.parseInt(input.getText().toString())

Since you are setting your TextView as empty with the code line:

input.setText("");
GVillani82
  • 17,196
  • 30
  • 105
  • 172