输入新号码时如何清除文字

时间:2017-03-12 19:23:53

标签: java android numberformatexception

我正在研究一个计算器。如果我没有输入任何数字并点击操作员,应用程序崩溃,你可以在下面看到原因。

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

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

有没有解决方法呢?

编辑 如果显示屏为空,我按下操作员按钮就会崩溃,因为我写了

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

这是我的计算代码。

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;


    }

错误

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 个答案:

答案 0 :(得分:0)

  

如果显示屏为空,我按下操作员按钮就会崩溃

因为Float.parseFloat不喜欢空字符串。

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

第一个方法调用可以/将失败。

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

您只需要先检查字符串是否为空并正确处理

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

如果你只想try {例外,你也可以将Float.parseFloat语句移到catch之上。

答案 1 :(得分:0)

您可以禁用操作员按钮,直到点击任何数字

或者您可以检查您创建的第一个和第二个数字。

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