gpt4 book ai didi

java - android 尝试乘法和显示 double 时出现错误

转载 作者:行者123 更新时间:2023-12-01 17:31:41 24 4
gpt4 key购买 nike

在尝试将 TextView 文本设置为数字时,由于某种原因,我总是遇到问题。在下面的代码中,我试图制作一个小费计算器,但是当我运行该应用程序时,它会强制关闭。当我删除 OnClick 监听器中的项目时,该应用程序运行得非常好。我将在代码之后发布 logcat。请告诉我任何其他有用的信息,我可以发布这些信息以提高我的问题得到解答的机会。

billAmount = (EditText) findViewById(R.id.billAmount);
tipPercent = (EditText) findViewById(R.id.tipPercent);

这些是编辑文本。

    @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();

最佳答案

使用billAmount.getText().toString();而不是billAmount.toString();

对于 tipPercent 也是如此。

关于java - android 尝试乘法和显示 double 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10264475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com