全部——我的项目(计算技巧)运行良好,直到我添加了一些 BigDecimal 变量来帮助舍入。现在,当我点击“计算”时,应用程序强制关闭,logcat 显示:
06-01 14:24:21.246: W/dalvikvm(15364): threadid=1: thread exiting with uncaught exception (group=0x40a4a1f8)
06-01 14:24:21.256: E/AndroidRuntime(15364): FATAL EXCEPTION: main
06-01 14:24:21.256: E/AndroidRuntime(15364): java.lang.IllegalStateException: Could not execute method of the activity
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.View$1.onClick(View.java:3044)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.View.performClick(View.java:3511)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.View.onKeyUp(View.java:6078)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.widget.TextView.onKeyUp(TextView.java:5636)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.KeyEvent.dispatch(KeyEvent.java:2623)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.View.dispatchKeyEvent(View.java:5500)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
06-01 14:24:21.256: E/AndroidRuntime(15364): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1879)
06-01 14:24:21.256: E/AndroidRuntime(15364): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
06-01 14:24:21.256: E/AndroidRuntime(15364): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3300)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.os.Looper.loop(Looper.java:137)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-01 14:24:21.256: E/AndroidRuntime(15364): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 14:24:21.256: E/AndroidRuntime(15364): at java.lang.reflect.Method.invoke(Method.java:511)
06-01 14:24:21.256: E/AndroidRuntime(15364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-01 14:24:21.256: E/AndroidRuntime(15364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-01 14:24:21.256: E/AndroidRuntime(15364): at dalvik.system.NativeStart.main(Native Method)
06-01 14:24:21.256: E/AndroidRuntime(15364): Caused by: java.lang.reflect.InvocationTargetException
06-01 14:24:21.256: E/AndroidRuntime(15364): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 14:24:21.256: E/AndroidRuntime(15364): at java.lang.reflect.Method.invoke(Method.java:511)
06-01 14:24:21.256: E/AndroidRuntime(15364): at android.view.View$1.onClick(View.java:3039)
06-01 14:24:21.256: E/AndroidRuntime(15364): ... 24 more
06-01 14:24:21.256: E/AndroidRuntime(15364): Caused by: java.lang.NumberFormatException: Invalid float: ""
06-01 14:24:21.256: E/AndroidRuntime(15364): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-01 14:24:21.256: E/AndroidRuntime(15364): at java.lang.StringToReal.parseFloat(StringToReal.java:289)
06-01 14:24:21.256: E/AndroidRuntime(15364): at java.lang.Float.parseFloat(Float.java:300)
06-01 14:24:21.256: E/AndroidRuntime(15364): at com.tip.ping.TippingActivity.calculateNumbers(TippingActivity.java:47)
06-01 14:24:21.256: E/AndroidRuntime(15364): ... 27 more
我的代码是:
public class TippingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void calculateNumbers(View view) {
EditText text = (EditText)findViewById(R.id.edit_bill);
String value;
value = text.getText().toString();
float bill = Float.parseFloat(value);
BigDecimal billbd;
billbd = new BigDecimal(bill);
EditText text2 = (EditText)findViewById(R.id.edit_tip);
String value2;
value2 = text2.getText().toString();
if(!value2.contains("."))
value2 = "."+value2;
float tip = Float.parseFloat(value2);
BigDecimal tipbd;
tipbd = new BigDecimal(tip);
EditText text3 = (EditText)findViewById(R.id.edit_people);
String value3;
value3 = text3.getText().toString();
TextView answer = (TextView) findViewById(R.id.answer);
String answers;
answers = answer.getText().toString();
float answerf = Float.parseFloat(answers);
BigDecimal answerbd;
answerbd = new BigDecimal(answerf);
answerbd.setScale(2);
TextView quick_answer = (TextView) findViewById(R.id.quick_answer);
String qanswers;
qanswers = quick_answer.getText().toString();
float qanswerf = Float.parseFloat(qanswers);
BigDecimal qanswerbd;
qanswerbd = new BigDecimal(qanswerf);
float l3 = Float.parseFloat(value3);
BigDecimal l3bd;
l3bd = new BigDecimal(l3);
qanswerbd = billbd.multiply(tipbd).add(billbd);
qanswerbd.setScale(2, RoundingMode.HALF_UP);
String resultbd;
resultbd = qanswerbd.toString();
quick_answer.setText(resultbd);
}
}
提前致谢!