1

My application works great while using the emulator, but it crashes while working on real device. I code it for Android 2.2. Real device is 2.3.6.

Problem is here:

When I use something like that:

tvPlayerOneScores.setText("-" + numberFormat.format(level));

it works great both on emulator and real device.

But when I try this:

tvPlayerOneScores.setText("-" + numberFormat.format(level * 0.5f));

it works on emulator, but crashes on real device.

Stack Trace:

java.lang.NumberFormatException
    at org.apache.harmony.luni.util.FloatingPointParser.parseFltImpl(Native Method)
    at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:321)
    at java.lang.Float.parseFloat(Float.java:323)
    at java.lang.Float.valueOf(Float.java:362)
    at com.amrotek.truthordare.GameActivity$4.onClick(GameActivity.java:287)
    at android.view.View.performClick(View.java:2552)
    at android.view.View$PerformClick.run(View.java:9229)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3701)
    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:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
    at dalvik.system.NativeStart.main(Native Method)

EDIT: I multiplied all values by 10, so I only work with integer numbers when I do arithmetic operations. At the end I just divide end value by 10. It works :)

4

1 回答 1

1

您应该在问题中包含错误的行,而不是在评论中:

playerOnePoints = Float.valueOf(numberFormat.format(playerOnePoints - level * 0.5f));

因为仅仅做出结论是不够的,这里有一个简短的清单供您检查:

  1. 为什么你需要格式化一个数字numberFormat只是为了将它转换回来valueOf

  2. 是什么numberFormat,它真的会产生有效的浮点字符串吗?

  3. playerOnePoints浮动还是浮动?你不应该使用parseFloat()代替valueOf()吗?

最后,如果您仍然无法解决您的问题,我建议将上述行替换为简单的:

playerOnePoints = playerOnePoints - level * 0.5f;

那应该可以解决问题=)

附言。Math.floor()/ceil()如果我正确理解您的意图,您也可能想熟悉parseFloat()/numberFormat()

于 2012-10-30T23:23:21.790 回答