2017-03-12 2 views
-1

Je travaille sur une calculatrice. Si je ne saisis aucun nombre et clique sur l'opérateur, l'application se bloque comme vous pouvez le voir ci-dessous pourquoi.Comment effacer le texte lors de la saisie d'un nouveau numéro

public void setOperator(String operatorCharacter){ 
    if (operatorState == false){ 
     displayHandler.clearRecorder(); 

    if (operatorState == true){ 
     calculate();* 

Y a-t-il une solution de contournement pour cela?

Modifier Si l'affichage est vide et j'appuie sur un bouton de l'opérateur, il se bloque parce que je l'ai écrit

if (operatorState == true){ 
     calculate();* 

Voici mon code de calcul.

public void calculate() { 
     seOp = Float.parseFloat(displayHandler.getDisplay().getText().toString()); 
     secondPacket = buttonManager.getExtSpinner().getSelectedItem().toString(); 
     ansPacket = buttonManager.getAnsSpinner().getSelectedItem().toString(); 

     try { 
      double targetPacket = conversion.getPacketSize(ansPacket); 
      double fiOpInTarget = 0d; 

      if (firstPacket != null) { 
       double firstOperandConversion = conversion.getPacketSize(firstPacket); 
       fiOpInTarget = (fiOp * targetPacket)/firstOperandConversion; 

      } 
      double secondOperandConversion = conversion.getPacketSize(secondPacket); 
      double seOpinTarget = (seOp * targetPacket)/secondOperandConversion; 

      if (operator.equals("+")) { 
       answerInTarget = fiOpInTarget + seOpinTarget; 
      } 
      else if (operator.equals("-")) { 
       answerInTarget = fiOpInTarget - seOpinTarget; 

      } else if (operator.equals("/")) { 
       answerInTarget = (fiOpInTarget/seOpinTarget); 

      } else if (operator.equals("x")) { 
       answerInTarget = fiOpInTarget * seOpinTarget; 

      } else { 
       answerInTarget = seOpinTarget; 
       displayHandler.addSingleValue(seOp, secondPacket); 
      } 
      displayHandler.setDisplay(String.format("%.2f", answerInTarget)); 
     } 
      catch (Exception e) { 
       e.printStackTrace(); 
       displayHandler.setDisplay("beep boop no internet. Please relaunch"); 
      } 
     operatorState = false; 


    } 

L'erreur

03-13 00:47:09.465 5467-5467/com.zeus.vibin.conv E/AndroidRuntime: FATAL EXCEPTION: main 
                   Process: com.zeus.vibin.conv, PID: 5467 
                   java.lang.IllegalStateException: Could not execute method for android:onClick 
                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 
                    at android.view.View.performClick(View.java:5198) 
                    at android.view.View$PerformClick.run(View.java:21147) 
                    at android.os.Handler.handleCallback(Handler.java:739) 
                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                    at android.os.Looper.loop(Looper.java:148) 
                    at android.app.ActivityThread.main(ActivityThread.java:5417) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                   Caused by: java.lang.reflect.InvocationTargetException 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                    at android.view.View.performClick(View.java:5198)  
                    at android.view.View$PerformClick.run(View.java:21147)  
                    at android.os.Handler.handleCallback(Handler.java:739)  
                    at android.os.Handler.dispatchMessage(Handler.java:95)  
                    at android.os.Looper.loop(Looper.java:148)  
                    at android.app.ActivityThread.main(ActivityThread.java:5417)  
                    at java.lang.reflect.Method.invoke(Native Method)  
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
                   Caused by: java.lang.NumberFormatException: Invalid float: "" 
                    at java.lang.StringToReal.invalidReal(StringToReal.java:63) 
                    at java.lang.StringToReal.parseFloat(StringToReal.java:308) 
                    at java.lang.Float.parseFloat(Float.java:306) 
                    at com.zeus.vibin.conv.button.math.Maths.setOperator(Maths.java:45) 
                    at com.zeus.vibin.conv.button.buttons.ButtonManager.operatorClick(ButtonManager.java:57) 
                    at com.zeus.vibin.conv.button.main.Convert.operatorClicked(Convert.java:33) 
                    at java.lang.reflect.Method.invoke(Native Method)  
                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  
                    at android.view.View.performClick(View.java:5198)  
                    at android.view.View$PerformClick.run(View.java:21147)  
                    at android.os.Handler.handleCallback(Handler.java:739)  
                    at android.os.Handler.dispatchMessage(Handler.java:95)  
                    at android.os.Looper.loop(Looper.java:148)  
                    at android.app.ActivityThread.main(ActivityThread.java:5417)  
                    at java.lang.reflect.Method.invoke(Native Method)  
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

Maths.java:45

displayHandler.addToRecorder(buttonManager.getExtSpinner().getSelectedItem().toString(), 
      Float.parseFloat(displayHandler.getDisplay().getText().toString()),operatorCharacter); 

ButtonManager.java:57

maths.setOperator(buttonText); 

Convert.java:33

buttonManager.operatorClick(v.getId()); 
+2

Le [logcat vous dira quelle raison votre application est arrêté] (http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) . S'il vous plaît [modifier] votre question avec elle. –

+1

Aussi, s'il vous plaît montrer un [mcve]. Rien de ce code n'a de sens hors contexte. –

+0

@ cricket_007 Il se bloque sur if (operatorState == true) { calculate(); –

Répondre

0

Si l'affichage est vide et j'appuie sur un bouton de l'opérateur, il se bloque

Parce que Float.parseFloat n'aime pas les chaînes vides.

Caused by: java.lang.NumberFormatException: Invalid float: ""

Ce premier appel de méthode peut échouer.

public void calculate() { 
    seOp = Float.parseFloat(displayHandler.getDisplay().getText().toString()); 

Il vous suffit de vérifier si la chaîne est vide d'abord et gérer de façon appropriée que

public void calculate() { 
    String op = displayHandler.getDisplay().getText().toString(); 
    if (!op.isEmpty()) { 
     try { 
      seOp = Float.parseFloat(op); 
     } catch (NumberFormatException e) { 
      seOp = 0.0; // some default value 
     } 
    } 
    secondPacket = buttonManager.getExtSpinner().getSelectedItem().toString(); 
    ansPacket = buttonManager.getAnsSpinner().getSelectedItem().toString(); 

Vous pouvez également déplacer la déclaration ci-dessus try {Float.parseFloat si vous ne vouliez juste catch l'exception.

+0

// peut encore échouer quand ce n'est pas un nombre > Cela fonctionne toujours. –

+0

Ensuite, relisez le logcat et lisez le message comme je l'ai indiqué. –

0

Vous pouvez désactiver les boutons de votre opérateur jusqu'à ce que cliquez sur les numéros,

ou vous pouvez vérifier votre premier et deuxième numéros pour créer ou non.

if(fiOpInTarget != null && seOpinTarget!= null) { 
    //do your calculations 
    if (operator.equals("+")) { 
    answerInTarget = fiOpInTarget + seOpinTarget; 
    } 
//so on 
} 
+0

opérateur! = Ne peut pas être appliqué à null/double. –