如果声明正常。它的代码如下所示
if (getInput1.getText() != null) {
float answer2 = Float.valueOf(getInput2.getText().toString());
float answer3 = Float.valueOf(getInput3.getText().toString());
float answer= answer2/answer3;
textOut1.setText(String.valueOf(answer));
}
然后我添加第二个if语句(下面的代码)
EditText getInput1 = (EditText) findViewById(R.id.etInput1);
EditText getInput2 = (EditText) findViewById(R.id.etInput2);
EditText getInput3 = (EditText) findViewById(R.id.etInput3);
if (getInput1.getText() != null) {
float answer2 = Float.valueOf(getInput2.getText().toString());
float answer3 = Float.valueOf(getInput3.getText().toString());
float answer= answer2/answer3;
textOut1.setText(String.valueOf(answer));
}
if (getInput2.getText()!= null ){
float answer1 = Float.valueOf(getInput1.getText().toString());
float answer3 = Float.valueOf(getInput3.getText().toString());
float answer= answer1*answer3;
textOut1.setText(String.valueOf(answer));
}
logCat说valueOf
函数出了问题。哪里可能是个错误?
logcat显示:
08-18 17:10:28.918: INFO/ActivityManager(569): Displayed activity com.easyPhys.start/.mechanics: 261 ms
08-18 17:10:29.648: INFO/ActivityManager(569): Starting activity: Intent { action=com.easyPhys.start.velocity comp={com.easyPhys.start/com.easyPhys.start.velocity} }
08-18 17:10:30.008: INFO/ActivityManager(569): Displayed activity com.easyPhys.start/.velocity: 366 ms
08-18 17:10:47.308: WARN/KeyCharacterMap(755): No keyboard for id 0
08-18 17:10:47.308: WARN/KeyCharacterMap(755): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-18 17:10:48.338: DEBUG/dalvikvm(704): GC freed 971 objects / 73712 bytes in 126ms
08-18 17:10:51.348: DEBUG/AndroidRuntime(755): Shutting down VM
08-18 17:10:51.348: WARN/dalvikvm(755): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
08-18 17:10:51.348: ERROR/AndroidRuntime(755): Uncaught handler: thread main exiting due to uncaught exception
08-18 17:10:51.368: ERROR/AndroidRuntime(755): java.lang.NumberFormatException:
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:296)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at java.lang.Float.parseFloat(Float.java:327)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at com.easyPhys.start.velocity$1.onClick(velocity.java:86)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.view.View.performClick(View.java:2179)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.view.View.onTouchEvent(View.java:3828)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.widget.TextView.onTouchEvent(TextView.java:6291)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.view.View.dispatchTouchEvent(View.java:3368)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1197)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.app.Activity.dispatchTouchEvent(Activity.java:1993)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.os.Looper.loop(Looper.java:123)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at java.lang.reflect.Method.invoke(Method.java:521)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-18 17:10:51.368: ERROR/AndroidRuntime(755): at dalvik.system.NativeStart.main(Native Method)