清除EditText时应用程序崩溃

时间:2015-07-28 10:09:51

标签: android button android-edittext

我正在制作这个试用游戏,在有人猜到一个数字之后,需要清除EditText视图。

这是我的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;
        }
    });
}

每当我尝试在我的软键盘上按Enter键或按下按钮时,它只是说" RaadHetGetal stopped。"

有什么想法吗?如果您还需要查看,请说出来。

2 个答案:

答案 0 :(得分:0)

在你的两个听众中,你至少有这个错误:

您将文本设置为空

input.setText("");

尝试直接从中解析一个int。这导致NumberFormatException

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

因此,如果您想在handleGuess之后清除文字,可以致电clear()

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

答案 1 :(得分:0)

是的,您获得java.lang.NumberFormatException,在第:

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

由于您使用代码行将TextView设置为空:

input.setText("");
相关问题