解析字符串加倍时的android错误

时间:2011-06-26 22:41:26

标签: android

enter code here

 06-27 00:34:04.641: ERROR/AndroidRuntime(13633): FATAL EXCEPTION: main
06-27 00:34:04.641: ERROR/AndroidRuntime(13633): java.lang.RuntimeException: Unable to start activity ComponentInfo{yaraby.y/yaraby.y.ghost}: java.lang.NumberFormatException: 
06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at android.app.ActivityThread.access$2300(ActivityThread.java:126)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at android.os.Looper.loop(Looper.java:123)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at android.app.ActivityThread.main(ActivityThread.java:4633)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at java.lang.reflect.Method.invokeNative(Native Method)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at java.lang.reflect.Method.invoke(Method.java:521)
06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
 06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at dalvik.system.NativeStart.main(Native Method)
  06-27 00:34:04.641: ERROR/AndroidRuntime(13633): Caused by: java.lang.NumberFormatException: 
  06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
 06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at java.lang.Double.parseDouble(Double.java:287)
 06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at yaraby.y.ghost.onCreate(ghost.java:89)
  06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
  06-27 00:34:04.641: ERROR/AndroidRuntime(13633):     ... 11 more

第89行

enter code here
  Double p1=    Double.parseDouble(x); 

我从服务器收到的是字符串,我希望它是双倍的,它已经是一个双号但是作为字符串接收

enter code here
 String x=inFromServer.readLine();
         String y=inFromServer.readLine();


            x=x.trim();
   Double p1=   Double.parseDouble(x);
                    y=y.trim();
        Double p2=   Double.parseDouble(y);

3 个答案:

答案 0 :(得分:2)

字符串是否包含“。” (如123.45)或“,”(如123,45)?

您应该根据您的区域设置替换它。尝试catch块也可以。

答案 1 :(得分:1)

在控制台上打印String x,同时确保为您的语言环境使用正确的小数点分隔符。是吗 。或者,?

在一个小程序中测试它。

此致  斯特凡

答案 2 :(得分:0)

Double.valueOf(string)返回Double
Double.parseDouble(string)返回基元类型 由于您要将结果分配给Double,因此最好使用
Double.valueOf(string)
无论如何我都无法破解代码,但我认为它值得一试。