android - 单击无值时出现计算器错误
问题描述
我需要帮助,我构建了计算器应用程序,但是当我单击没有值的加、减或等于按钮时,应用程序将崩溃。否则,如果我先插入值,则运行无错误。帮助
等号按钮
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)
解决方案
这是因为您试图解析一个空字符串,这不是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;
}
}
推荐阅读
- phpmailer - Cannot modify header information - headers already sent in php mailer
- php - 如何从 phpUnit Mock 对象中获取私有属性
- dart - Flutter FilterChip _cast 列表从端点动态加载
- javascript - 刷新页面时,Reactjs 道具不会填充字段
- package.json - 如何在不考虑 package.json 版本范围的情况下将包升级到最新版本?
- django - Django apache2 url直接没有基本url
- javascript - 新的 FB UI 元素提升/降级做什么
- machine-learning - SVM 加载的训练模型的准确率明显降低;我们也需要保存 TokenCountVectorizer 吗?
- symfony - 重置 Symfony 会话生命周期/用户活动超时
- ios - iOS:我收到一条错误消息“请求的属性表达式的地址”