-2

这是我转换为整数的代码

 String pricevalue=i.getStringExtra("info");
        int start = pricevalue.indexOf("price:");
       int index=pricevalue.indexOf("$");

       price=pricevalue.subSequence(index,pricevalue.length()-1).toString();



 int p1=Integer.parseInt(price.toString());//error is here

我的日志猫错误

11-24 10:51:32.002: E/AndroidRuntime(549): FATAL EXCEPTION: main
11-24 10:51:32.002: E/AndroidRuntime(549): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.orderplacemnet/com.example.orderplacemnet.YourTotalBill}: java.lang.NumberFormatException: unable to parse '$34.00' as integer
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.os.Looper.loop(Looper.java:123)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-24 10:51:32.002: E/AndroidRuntime(549):  at java.lang.reflect.Method.invokeNative(Native Method)
11-24 10:51:32.002: E/AndroidRuntime(549):  at java.lang.reflect.Method.invoke(Method.java:521)
11-24 10:51:32.002: E/AndroidRuntime(549):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-24 10:51:32.002: E/AndroidRuntime(549):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-24 10:51:32.002: E/AndroidRuntime(549):  at dalvik.system.NativeStart.main(Native Method)
11-24 10:51:32.002: E/AndroidRuntime(549): Caused by: java.lang.NumberFormatException: unable to parse '$34.00' as integer
11-24 10:51:32.002: E/AndroidRuntime(549):  at java.lang.Integer.parse(Integer.java:433)
11-24 10:51:32.002: E/AndroidRuntime(549):  at java.lang.Integer.parseInt(Integer.java:422)
11-24 10:51:32.002: E/AndroidRuntime(549):  at java.lang.Integer.parseInt(Integer.java:382)
11-24 10:51:32.002: E/AndroidRuntime(549):  at com.example.orderplacemnet.YourTotalBill.onCreate(YourTotalBill.java:51)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-24 10:51:32.002: E/AndroidRuntime(549):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-24 10:51:32.002: E/AndroidRuntime(549):  ... 11 more
4

2 回答 2

1

price=pricevalue.subSequence(index+1,pricevalue.length()-1);
Double p1=Double.parseDouble(price);

代替

price=pricevalue.subSequence(index,pricevalue.length()-1).toString();
int p1=Integer.parseInt(price.toString());

也许它会解决你的问题。

于 2012-11-24T05:43:22.550 回答
0

我改变了代码现在试试

String pricevalue=i.getStringExtra("info");
int start = pricevalue.indexOf("price:");
int index=pricevalue.indexOf("$");
price=pricevalue.subSequence(index,pricevalue.length()-1).toString();
int p1=Integer.parseInt(price.replaceAll("[\\D]", ""))
于 2012-11-24T05:41:37.107 回答