Android Try/Catch numberformatexception中的Toast

Android Try/Catch numberformatexception中的Toast,android,try-catch,android-alertdialog,toast,numberformatexception,Android,Try Catch,Android Alertdialog,Toast,Numberformatexception,我试图在Try/Catch中使用Toast,但当它捕获到错误的输入时,Toast不会出现。我在代码中找不到错误,请在这里帮助我 这是代码的一小部分,包括try/catch块: else{ try{ Integer.parseInt(score_1.getText().toString()); Integer.parseInt(score_2.getText().toString()); ArrayList_matches.

我试图在Try/Catch中使用Toast,但当它捕获到错误的输入时,Toast不会出现。我在代码中找不到错误,请在这里帮助我

这是代码的一小部分,包括try/catch块:

 else{
      try{
          Integer.parseInt(score_1.getText().toString());
          Integer.parseInt(score_2.getText().toString());
          ArrayList_matches.get(position_match).score_1 = score_1.getText().toString();
          ArrayList_matches.get(position_match).score_2 = score_2.getText().toString();
          adapter.notifyDataSetChanged();


      } catch(NumberFormatException ex) {
          Toast.makeText(Game_activity.this, "Wrong input, numbers only", Toast.LENGTH_LONG).show();
      }

     }
这么简单的使用

Toast.makeText(getApplicationContext(), "Wrong input, numbers only", Toast.LENGTH_LONG).show();
而不是

Toast.makeText(Game_activity.this, "Wrong input, numbers only", Toast.LENGTH_LONG).show();
如果要调试,请使用

Log.d("TAG","YOUR MSG");

为score1设置一些默认整数值。

试试这种方法

 try {

     Integer.parseInt(score_1.getText().toString());
     Integer.parseInt(score_2.getText().toString());
     ArrayList_matches.get(position_match).score_1 = score_1.getText().toString();
     ArrayList_matches.get(position_match).score_2 = score_2.getText().toString();
     adapter.notifyDataSetChanged();

 } catch(NumberFormatException ex) {

     runOnUiThread(new Runnable(){
     public void run() {
         Toast.makeText(Game_activity.this, "Wrong input, numbers only", Toast.LENGTH_LONG).show();
     });

 }
我强烈建议使用
Log
insidecatch


快乐编码。

可能不是数字格式异常。请输入非数字格式的错误信息,

您应该使用log.e(标签、消息);