在尝试将 textviews 文本设置为数字时,由于某种原因,我总是遇到问题。在下面的代码中,我试图制作一个小费计算器,但是当我运行应用程序时,它会强制关闭。当我删除 OnClick 侦听器中的项目时,该应用程序运行良好。我将在代码之后发布 logcat。请告诉我任何其他有用的信息,我可以发布以提高我的问题得到回答的机会。
billAmount = (EditText) findViewById(R.id.billAmount);
tipPercent = (EditText) findViewById(R.id.tipPercent);
这些是editTexts。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.calculateTip:
double billAmt = Double.parseDouble(billAmount.toString());
double tipAmt = Double.parseDouble(tipPercent.toString());
double billtip = (billAmt * tipAmt) / 100;
totalTip.setText("" + Double.toString(billtip));
break;
}
}
这是日志猫
04-22 01:20:20.104: W/KeyCharacterMap(589): No keyboard for id 0
04-22 01:20:20.104: W/KeyCharacterMap(589): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-22 01:20:24.174: D/AndroidRuntime(589): Shutting down VM
04-22 01:20:24.174: W/dalvikvm(589): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-22 01:20:24.195: E/AndroidRuntime(589): FATAL EXCEPTION: main
04-22 01:20:24.195: E/AndroidRuntime(589): java.lang.NumberFormatException: android.widget.EditText@44f54a78
04-22 01:20:24.195: E/AndroidRuntime(589): at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:130)
04-22 01:20:24.195: E/AndroidRuntime(589): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:281)
04-22 01:20:24.195: E/AndroidRuntime(589): at java.lang.Double.parseDouble(Double.java:287)
04-22 01:20:24.195: E/AndroidRuntime(589): at com.tool.TipCalculator.onClick(TipCalculator.java:38)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.view.View.performClick(View.java:2408)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.view.View$PerformClick.run(View.java:8816)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.os.Handler.handleCallback(Handler.java:587)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.os.Handler.dispatchMessage(Handler.java:92)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.os.Looper.loop(Looper.java:123)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-22 01:20:24.195: E/AndroidRuntime(589): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 01:20:24.195: E/AndroidRuntime(589): at java.lang.reflect.Method.invoke(Method.java:521)
04-22 01:20:24.195: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-22 01:20:24.195: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-22 01:20:24.195: E/AndroidRuntime(589): at dalvik.system.NativeStart.main(Native Method)
我很确定我已经解决了这个问题。只是在做
Double.parseDouble(editText.toString();
不工作。我需要添加
Double.parseDouble(editText.getText().toString();