私はこのコードを試しました(関連部分..):
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() メソッドを使用していますが...
ちなみに、両方のフィールドにゼロの値を入力すると、正常に動作します (適切なトースト メッセージ) アイデアはありますか?... ありがとうございます! :-(