我试过这段代码(相关部分..):
private EditText weightEditText,heightEditText;
weightEditText = (EditText)findViewById(R.id.weightEditText);
heightEditText = (EditText)findViewById(R.id.heightEditText);
public void onClick(View v) {
switch (v.getId()){
case R.id.clear: clearFields();toastClearingMessage(); break;
case R.id.goButton: runGoButton(); break;
}//switch
}
private void runGoButton() {
if (heightWeightFieldsValuesGood() && heightWeightFieldsNotEmpty() )
runCalculations();
else
toastMessageReEnterValues();
}
private boolean heightWeightFieldsValuesGood() {
if (getWeight()>0 && getHeight()>0)
return true;
return false;
}
private boolean heightWeightFieldsNotEmpty(){
if ( (weightEditText.getText().toString().trim().equals("")) || (heightEditText.getText().toString().trim().equals("")) )
return false;
return true;
}
private float getHeight() {
return Float.parseFloat(heightEditText.getText().toString());
}
private float getWeight() {
return Float.parseFloat(weightEditText.getText().toString());
}
崩溃日志:
10-08 01:21:54.260: W/dalvikvm(8172): threadid=1: thread exiting with uncaught exception (group=0x40020ac0)
10-08 01:21:54.260: E/AndroidRuntime(8172): FATAL EXCEPTION: main
10-08 01:21:54.260: E/AndroidRuntime(8172): java.lang.NumberFormatException:
10-08 01:21:54.260: E/AndroidRuntime(8172): at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:305)
10-08 01:21:54.260: E/AndroidRuntime(8172): at java.lang.Float.parseFloat(Float.java:291)
10-08 01:21:54.260: E/AndroidRuntime(8172): at com.example.bmi.calculator.MainPage.getWeight(MainPage.java:122)
10-08 01:21:54.260: E/AndroidRuntime(8172): at com.example.bmi.calculator.MainPage.heightWeightFieldsValuesGood(MainPage.java:75)
10-08 01:21:54.260: E/AndroidRuntime(8172): at com.example.bmi.calculator.MainPage.runGoButton(MainPage.java:63)
10-08 01:21:54.260: E/AndroidRuntime(8172): at com.example.bmi.calculator.MainPage.onClick(MainPage.java:58)
10-08 01:21:54.260: E/AndroidRuntime(8172): at android.view.View.performClick(View.java:2420)
10-08 01:21:54.260: E/AndroidRuntime(8172): at android.view.View$PerformClick.run(View.java:8941)
10-08 01:21:54.260: E/AndroidRuntime(8172): at android.os.Handler.handleCallback(Handler.java:587)
10-08 01:21:54.260: E/AndroidRuntime(8172): at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 01:21:54.260: E/AndroidRuntime(8172): at android.os.Looper.loop(Looper.java:123)
10-08 01:21:54.260: E/AndroidRuntime(8172): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-08 01:21:54.260: E/AndroidRuntime(8172): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 01:21:54.260: E/AndroidRuntime(8172): at java.lang.reflect.Method.invoke(Method.java:521)
10-08 01:21:54.260: E/AndroidRuntime(8172): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-08 01:21:54.260: E/AndroidRuntime(8172): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-08 01:21:54.260: E/AndroidRuntime(8172): at dalvik.system.NativeStart.main(Native Method)
一切都运行得很好,但是如果我单击 GO 按钮而不插入任何字段值,程序就会崩溃并关闭......虽然我使用的是 trim() 方法......
顺便说一句,如果我在两个字段中输入零值,它工作正常(适当的 Toast 消息)有什么想法吗?...谢谢!:-(