2016-08-03 33 views
-1

我想在我的Try/Catch中使用Toast,但是當它捕捉到錯誤的輸入時,Toast不會顯示出來。我找不到代碼中的錯誤,請在這裏幫助我。Toast in Try/Catch numberformatexception

這其中包括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(); 
     } 

    } 
+0

您應該使用log.e(tag,message); – sonnv1368

回答

0

因此簡單的用

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"); 
0

爲score1設置一些默認整數值。

0

嘗試這種方式

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內捕撈。

快樂編碼。

0

可能不是NumberFormatException。請輸入非數字格式的錯誤輸入,