今天接到一个波兰的客户说有个APP在英文状态下一切运行正常,但是当系统语言切换到波兰语言的时候,程序奔溃了。好吧,又是我来维护。
好吧,先把系统语言切换到波兰语,切换到波兰语的方法查看文章
我的Android进阶之旅------>Android【设置】-【语言和输入法】-【语言】列表中找到相应语言所对应的列表项
地址:http://blog.csdn.net/ouyang_peng/article/details/50209789
====================================================================================
报错如下:
D/AndroidRuntime( 9067): Shutting down VM
E/AndroidRuntime( 9067): FATAL EXCEPTION: main
E/AndroidRuntime( 9067): Process: com.runbo.outdoormeter, PID: 9067
E/AndroidRuntime( 9067): java.lang.NumberFormatException: Invalid float: "1019,35"
E/AndroidRuntime( 9067): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
E/AndroidRuntime( 9067): at java.lang.StringToReal.initialParse(StringToReal.java:164)
E/AndroidRuntime( 9067): at java.lang.StringToReal.parseFloat(StringToReal.java:323)
E/AndroidRuntime( 9067): at java.lang.Float.parseFloat(Float.java:306)
E/AndroidRuntime( 9067): at java.lang.Float.valueOf(Float.java:343)
E/AndroidRuntime( 9067): at com.runbo.outdoormeter.service.DataService.onSensorChanged(DataService.java:66)
E/AndroidRuntime( 9067): at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:405)
E/AndroidRuntime( 9067): at android.os.MessageQueue.nativePollOnce(Native Method)
E/AndroidRuntime( 9067): at android.os.MessageQueue.next(MessageQueue.java:148)
E/AndroidRuntime( 9067): at android.os.Looper.loop(Looper.java:151)
E/AndroidRuntime( 9067): at android.app.ActivityThread.main(ActivityThread.java:5637)
E/AndroidRuntime( 9067): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 9067): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 9067): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
E/AndroidRuntime( 9067): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
好吧,数字格式异常,java.lang.NumberFormatException: Invalid float: "1019,35" ,怎么浮点型1019.35就变成了1019,35 小数点(".")变成了逗号(“,”)。查看源代码,代码如下:
LocationApplication.dangxia_qiya=event.values[0];
Log.i("sensor","senser is running 1.....");
float a1=LocationApplication.dangxia_qiya;
String aa1=String.format("%.2f",a1);
Log.i("sensor","a1="+a1);
Log.i("sensor","aa1="+aa1);
LocationApplication.dangxia_qiya=Float.valueOf(aa1);
C:\Documents and Settings\Administrator>adb logcat -s sensor
--------- beginning of system
--------- beginning of main
I/sensor ( 8175): senser is running 1.....
I/sensor ( 8175): a1=1019.00757
I/sensor ( 8175): aa1=1019,01
这说明,得到的a1参数是正常的,只是通过String.format方法转换后,就变成了1019,01。坑爹啊!将系统语言切换为英文,再打印下日志,如下:
C:\Documents and Settings\Administrator>adb logcat -s sensor
--------- beginning of system
--------- beginning of main
I/sensor ( 8648): senser is running 1.....
I/sensor ( 8648): a1=1019.01685
I/sensor ( 8648): aa1=1019.02
好吧,
得到的a1参数是正常的,aa1也是正常的1019.02。
====================================================================================
好吧,看了代码后,感觉用String.format来格式化浮点型数字真心不给力。这个保留小数点后两位的问题还是用其他方法来解决吧。将代码改成下面代码后一切正常。
LocationApplication.dangxia_qiya=event.values[0];
Log.i("sensor","senser is running 1.....");
float a1=LocationApplication.dangxia_qiya;
//String aa1=String.format("%.2f",a1);
//Log.i("sensor","a1="+a1);
//Log.i("sensor","aa1="+aa1);
//LocationApplication.dangxia_qiya= Float.valueOf(aa1);
float aa1 = (float)(Math.round(a1*100))/100;
Log.i("sensor","a1="+a1);
Log.i("sensor","aa1="+aa1);
LocationApplication.dangxia_qiya=aa1;
====================================================================================
不知道String.format()这样的错误,是不是java设计时候的一个缺陷呢?
====================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
====================================================================================