Android 应用程序可以在模拟器上工作,但不能在真正的设备-浮点型上工作
我的应用程序在使用仿真器时运行良好,但在实际设备上运行时崩溃。我为Android 2.2编写了代码。真正的设备是2.3.6 问题就在这里: 当我使用这样的东西时:Android 应用程序可以在模拟器上工作,但不能在真正的设备-浮点型上工作,android,Android,我的应用程序在使用仿真器时运行良好,但在实际设备上运行时崩溃。我为Android 2.2编写了代码。真正的设备是2.3.6 问题就在这里: 当我使用这样的东西时: tvPlayerOneScores.setText("-" + numberFormat.format(level)); 它在模拟器和真实设备上都非常有效 但当我尝试这个: tvPlayerOneScores.setText("-" + numberFormat.format(level * 0.5f)); 它在模拟器上工作,但在
tvPlayerOneScores.setText("-" + numberFormat.format(level));
它在模拟器和真实设备上都非常有效
但当我尝试这个:
tvPlayerOneScores.setText("-" + numberFormat.format(level * 0.5f));
它在模拟器上工作,但在真实设备上崩溃
堆栈跟踪:
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)
编辑:
我将所有的值乘以10,所以我在做算术运算时只处理整数。在最后,我只需将结束值除以10。有效:)您应该在问题中包括故障线路,而不是在注释中:
playerOnePoints = Float.valueOf(numberFormat.format(playerOnePoints - level * 0.5f));
由于这还不足以得出结论,这里有一个简短的清单供您检查:
numberFormat
格式化一个数字,然后用valueOf
将其转换回来numberFormat
,它真的会产生有效的浮点字符串吗parseFloat()
而不是valueOf()
playerOnePoints = playerOnePoints - level * 0.5f;
这就应该奏效了
另外,如果我正确理解了你的意图,你可能还想了解一下
Math.floor()/ceil()
。你应该在问题中包括错误的行,而不是在评论中:
playerOnePoints = Float.valueOf(numberFormat.format(playerOnePoints - level * 0.5f));
由于这还不足以得出结论,这里有一个简短的清单供您检查:
numberFormat
格式化一个数字,然后用valueOf
将其转换回来numberFormat
,它真的会产生有效的浮点字符串吗parseFloat()
而不是valueOf()
playerOnePoints = playerOnePoints - level * 0.5f;
这就应该奏效了
另外,如果我正确地理解了你的意图,你可能还想熟悉一下
Math.floor()/ceil()
。你有什么例外?发布crashYep的堆栈跟踪,需要像raghav提到的那样的堆栈跟踪,哪一行是GameActivity.java:287?@m0skit0第287行:playerPoints=Float.valueOf(numberFormat.format(playerPoints-level*0.5f))代码>很遗憾,有了这些信息,我们无法帮助您。这实际上没有告诉我们任何关于价值观的信息。你调试过了吗?有什么异常?发布crashYep的堆栈跟踪,需要像raghav提到的那样的堆栈跟踪,哪一行是GameActivity.java:287?@m0skit0第287行:playerPoints=Float.valueOf(numberFormat.format(playerPoints-level*0.5f))代码>很遗憾,有了这些信息,我们无法帮助您。这实际上没有告诉我们任何关于价值观的信息。你调试过了吗?