首页 > 解决方案 > 单击无值时出现计算器错误

问题描述

我需要帮助,我构建了计算器应用程序,但是当我单击没有值的加、减或等于按钮时,应用程序将崩溃。否则,如果我先插入值,则运行无错误。帮助

等号按钮

FloatingActionButton fab = findViewById(R.id.fab);
fab.bringToFront();
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Value2 = Float.parseFloat(ed1.getText() + "" );
        if (mAddition == true){
            ed1.setText(Value1 + Value2 +"");
            mAddition=false;
        }
        if (mSubtract == true){
            ed1.setText(Value1 - Value2 +"");
            mSubtract=false;
        }
    }
}); 

子按钮

FloatingActionButton btn_Sub = findViewById(R.id.btn_Sub);
btn_Sub.bringToFront();
btn_Sub.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Value1 = Float.parseFloat(ed1.getText() + "");
        mSubtract = true ;
        ed1.setText("");
    }
});

添加按钮

FloatingActionButton btn_Add = findViewById(R.id.btn_Add);
btn_Add.bringToFront();
btn_Add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (ed1 == null){
            ed1.setText("");
        }else {
            Value1 = Float.parseFloat(ed1.getText() + "");
            mAddition = true;
            ed1.setText("");
        }
    }
});

我应该怎么办?

这是我的日志猫

 04-29 22:27:18.861 10722-10722/afif.calfits V/FA: onActivityCreated
04-29 22:27:18.891 10722-10722/afif.calfits D/skia_ext: turboImageDecoder took 27.87ms to decode 1024x768 (1x downscale)
04-29 22:27:19.151 10722-10744/afif.calfits V/FA: Activity resumed, time: 5774739
04-29 22:27:19.151 10722-10744/afif.calfits D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=Food, firebase_previous_id(_pi)=-2803431669891477858, firebase_screen_class(_sc)=Dairy, firebase_screen_id(_si)=-8225789602144573450}]
04-29 22:27:20.261 10722-10722/afif.calfits D/AndroidRuntime: Shutting down VM
04-29 22:27:20.261 10722-10722/afif.calfits E/AndroidRuntime: FATAL EXCEPTION: main
                                                              Process: afif.calfits, PID: 10722
                                                              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 afif.calfits.Dairy$4.onClick(Dairy.java:95)
                                                                  at android.view.View.performClick(View.java:4756)
                                                                  at android.view.View$PerformClick.run(View.java:19761)
                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                  at android.app.ActivityThread.main(ActivityThread.java:5253)
                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

标签: androidandroid-studio

解决方案


这是因为您试图解析一个空字符串,这不是Float.parseFloat()方法的正确参数。从以下文档

公共静态浮点 parseFloat(String s) 抛出 NumberFormatException

返回一个新的浮点数,初始化为指定字符串表示的值,由 Float 类的 valueOf 方法执行。

参数:
s - 要解析的字符串。
返回:
由字符串参数表示的浮点值。
抛出:
NullPointerException - 如果字符串为 null
NumberFormatException - 如果字符串不包含可解析的浮点数。

您可以看到NullPointerException如果字符串为空或NumberFormatException字符串不是浮点数,该方法将抛出。空字符串不是浮点数,因此您将收到NumberFormatException

因此,您需要检查空字符串并使用以下内容捕获错误:

String text = ed1.getText().toString();
float value;

// check if text is empty or not.
if(text.isEmpty()) {
  value = 0;
} else {
  try {
    value = Float.parseFloat(text);    
  } catch(NumberFormatException e) {
    // text is not a number, set it as 0.
    value = 0;
  }
}

推荐阅读