2016-08-03 1 views
-1

Je suis impatient d'utiliser un Toast dans mon Try/Catch mais quand il attrape une mauvaise entrée, le toast n'apparaît pas. Je ne peux pas trouver l'erreur dans le code, s'il vous plaît aidez-moi ici.Toast dans Try/Catch numberformatexception

c'est une courte partie du code qui comprend le bloc 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

Vous devez utiliser log.e (étiquette, message); – sonnv1368

Répondre

0

Si simple utilisation

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

au lieu de

Toast.makeText(Game_activity.this, "Wrong input, numbers only", Toast.LENGTH_LONG).show(); 

Et si vous voulez déboguer puis utiliser

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

Définissez des valeurs d'entier par défaut pour score1.

0

Essayez cette façon

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(); 
    }); 

} 

Je recommande fortement à utiliser Log prises à l'intérieur.

Bonne codification.

0

peut ne pas être une exception NumberFormatException. veuillez donner une mauvaise entrée qui n'est pas au format numérique,