2013-01-22 8 views
-1

J'ai 2 champs edittext vides et je veux quand je tape des nombres à l'intérieur des deux champs une 3ème textview obtiendra automatiquement la division des 2 ci-dessus edittexts. Donc, pas de boutons pour calculer que etcmise à jour textview sans aucun bouton

Le truc, c'est que quand j'entre un numéro sur un champ, l'application se ferme. Voici mon code et le logcat:

amount.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable arg0) { 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      AddFuelActivity.this.updateValue(); 
     } 

    }); 

    litres.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable arg0) { 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      AddFuelActivity.this.updateValue(); 
     } 

    }); 


protected void updateValue() { 
    int amountInt = Integer.parseInt(amount.getText().toString()); 
    int litresInt = Integer.parseInt(litres.getText().toString()); 
    float priceFuelAuto = 0; 
    priceFuelAuto = amountInt/litresInt; 
    fuelPrice.setText("€ " + priceFuelAuto); 
} 

erreurs logcat: je pense que ce sont les plus importants.

01-22 13:23:21.650: E/AndroidRuntime(671): java.lang.NumberFormatException: Invalid int: "" 
01-22 13:23:21.650: E/AndroidRuntime(671): at java.lang.Integer.invalidInt(Integer.java:138) 
01-22 13:23:21.650: E/AndroidRuntime(671): at java.lang.Integer.parseInt(Integer.java:359) 
01-22 13:23:21.650: E/AndroidRuntime(671): at java.lang.Integer.parseInt(Integer.java:332) 
01-22 13:23:21.650: E/AndroidRuntime(671): atcom.example.mycarfuel.AddFuelActivity.updateValue(AddFuelActivity.java:155) 
01-22 13:23:21.650: E/AndroidRuntime(671): atcom.example.mycarfuel.AddFuelActivity$2.onTextChanged(AddFuelActivity.java:67) 

J'ai corrigé mon code. Voici ce que je l'ai fait:

public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     if (litres.length() > 0) { 
      AddFuelActivity.this.updateValue(); 
     } 
    } 

et

int amountInt = 0; 
    int litresInt = 0; 
    try { 
     amountInt = Integer.parseInt(amount.getText().toString()); 
     litresInt = Integer.parseInt(litres.getText().toString()); 
    } catch (NumberFormatException e) { 
     litresInt = 0; 
     amountInt = 0; 
    } 
    if(amountInt !=0 && litresInt != 0){ 
     float priceFuelAuto = 0; 
     priceFuelAuto = amountInt/litresInt; 
     fuelPrice.setText("€ " + priceFuelAuto); 
    } 

merci pour vos réponses personnes

+1

Son bcoz vous essayer de comparer une chaîne vide qui n'est pas un nombre entier valide. –

+0

oui la chose est qu'il a capturé et a arrêté mon programme dès que je suis entré seulement 1 nombre. Thats y étaient tous 0. – lantonis

Répondre

0
@Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      // TODO Auto-generated method stub 
      if(edittext1.length()!=0){ 
      updateValue() 

      } 
      else{ 
       edittext2.setText(""); 
      } 
0

il suffit d'écrire ci-dessous le code ..

amount.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable arg0) { 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      if(s.length()>0)  <<<<===Just put this condition 
      AddFuelActivity.this.updateValue(); 
     } 

    }); 

    litres.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable arg0) { 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, 
       int count) 
     { 
      if(s.length()>0)  <<<<===Just put this condition 
      AddFuelActivity.this.updateValue(); 
     } 

    }); 


protected void updateValue() { 
    int amountInt = Integer.parseInt(amount.getText().toString()); 
    int litresInt = Integer.parseInt(litres.getText().toString()); 
    float priceFuelAuto = 0; 
    priceFuelAuto = amountInt/litresInt; 
    fuelPrice.setText("€ " + priceFuelAuto); 
} 
Questions connexes