需要帮助! 我试图为一个空的edittext做吐司,并且结果是继续得到浮动的错误。
请告知
这是代码:
calcBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
**float bs = Float.parseFloat(basicSalary.getText().toString());**
float tw = Float.parseFloat(totalAllowance.getText().toString());
float mob = String spinTEXT = spinner.getSelectedItem().toString();
if (spinner.getSelectedItem().equals("Select age group")){
Toast.makeText(sghospitalbonuscalculator.this, "Test 123",
Toast.LENGTH_SHORT).show();
}
if (basicSalary.getText().toString().equals("")){
Toast.makeText(sghospitalbonuscalculator.this, "Test 321",
Toast.LENGTH_SHORT).show();
}
这是错误:
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 sg.self.wser.bonuscalc.bonuscalcu$4.onClick(bonuscalcu.java:96)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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)
bonuscalc.java:96指的是这行代码:
float bs = Float.parseFloat(basicSalary.getText()。toString());
至于吐司,我试图将.equals(“”)更改为.equals(“”),. equals(null)和.isEmpty,但它仍然无法正常工作。
请指教! 太感谢了!
编辑版:
代码:
if (basicSalary.getText().toString().equals(0.0)){
Toast.makeText(sghospitalbonuscalculator.this, "Test 321",
Toast.LENGTH_SHORT).show();
}
显示的错误:
java.lang.NumberFormatException: Invalid float: ""
答案 0 :(得分:0)
您想测试输入是否为空,如果是,请指定值0.0
float bs = 0.0f;
if (basicSalary.getText().toString().equals("") == false){
bs = Float.parseFloat(basicSalary.getText().toString());
}
进一步向下测试Toast
if (basicSalary.getText().toString().equals("")){
Toast.makeText(sghospitalbonuscalculator.this, "Test 321",
Toast.LENGTH_SHORT).show();
}
也许你可以把它改成
if (bs == 0.0f){
Toast.makeText(sghospitalbonuscalculator.this, "Test 321",
Toast.LENGTH_SHORT).show();
}