NumberFormatException - Android

发布于 2024-11-07 09:29:42 字数 2008 浏览 2 评论 0原文

我今天已将 Android 应用上传到市场,收到 4 个有关 NumberFormatException,Double.parseDoubleDouble.valueOf 的错误。

这是我在开发者控制台中看到的 Logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{can.you.drive/can.you.drive.do_drive}: java.lang.NumberFormatException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException
at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
at java.lang.Double.parseDouble(Double.java:318)
at java.lang.Double.valueOf(Double.java:356)
at can.you.drive.dont_drive.roundTwoDecimals(dont_drive.java:132)
at can.you.drive.do_drive.onCreate(do_drive.java:125)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)

这是发生错误的函数(根据日志) -

public static double roundTwoDecimals(double d) {
    DecimalFormat twoDForm = new DecimalFormat("#.###");
return Double.valueOf(twoDForm.format(d));
}

我知道 d 是双精度的。

另外,这在我的手机上运行良好(Nexus S 运行 2.3.4)。我从其他用户那里收到错误。这就是为什么我无法真正调试它。

我真的不知道在某些手机上出现这种情况的原因是什么。

I've uploaded my Android app to the Market today, and I received 4 errors regarding NumberFormatException,Double.parseDouble and Double.valueOf.

Here's the Logcat I see in the Developer Console:

java.lang.RuntimeException: Unable to start activity ComponentInfo{can.you.drive/can.you.drive.do_drive}: java.lang.NumberFormatException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException
at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
at java.lang.Double.parseDouble(Double.java:318)
at java.lang.Double.valueOf(Double.java:356)
at can.you.drive.dont_drive.roundTwoDecimals(dont_drive.java:132)
at can.you.drive.do_drive.onCreate(do_drive.java:125)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)

Here's the function in which the error occurs(according to the Log) -

public static double roundTwoDecimals(double d) {
    DecimalFormat twoDForm = new DecimalFormat("#.###");
return Double.valueOf(twoDForm.format(d));
}

I know that d is double.

Also, this works fine on my phone(Nexus S running 2.3.4). I'm getting the error from other users. That's why I can't really debug it.

I really don't know what my cause this on some phones.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴骨ら 2024-11-14 09:29:42

我想说这与本地化有关, . 不是发生错误的平台中的小数分隔符。为什么不使用更便宜的舍入方法?

public double round(double d, int nDecimals)
{
    for(int i=0; i<nDecimals; ++i) d *= 10d;
    d=Math.round(d);
    for(int i=0; i<nDecimals; ++i) d /= 10d;
    return d;
}

I'd say it's got to do with localization, with . not being the decimal separator in the platforms where the error occurs. Why don't you use a less expensive method of rounding?

public double round(double d, int nDecimals)
{
    for(int i=0; i<nDecimals; ++i) d *= 10d;
    d=Math.round(d);
    for(int i=0; i<nDecimals; ++i) d /= 10d;
    return d;
}
惜醉颜 2024-11-14 09:29:42

使用 try catch 块来处理它。

use try catch block to handle it.

狼亦尘 2024-11-14 09:29:42

当使用德语的人使用您的应用程序时,可能会发生错误。我遇到过这个问题。他们使用逗号代替小数点(“,”而不是“.”)。这让我多次失望

Perhaps the error occurs when someone from the Deutsch language uses your application. I've had this problem. They use a comma in place of a decimal (',' instead of '.'). This has thrown me off multiple times

假装不在乎 2024-11-14 09:29:42

要调试它,请在代码中添加大量 System.out.println(),以便在变量更改时输出变量。这些比 gdb 等工具更容易。

To debug it, add a ton of System.out.println()'s all over your code that spit out a variable when it is changed. These are easier than tools like gdb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文