2013-04-30 74 views
2

我寫一個應用車中,我需要一個活動,已經通過點擊列表視圖中,用戶打開特定項目。列表視圖項排點擊產生的原因:java.lang.NumberFormatException

我使用列表視圖兩種不同的活動,一個顯示所選項目(S)即CartActivity.java和第二另一個動作表現出任何所選擇的項目即ProductInformationActivity.java

我已經寫代碼來調用一個活動,已經由用戶選擇的特定項目,在列表視圖CartActivity.java

// below is the line number 77 

    itemamount = Double.parseDouble(text_cost_code.getText().toString()); 

logcat的表示:

04-30 14:37:10.073: E/AndroidRuntime(273): FATAL EXCEPTION: main 
04-30 14:37:10.073: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.era.restaurant.versionoct/com.era.restaurant.versionoct.menu.ProductInformationActivity}: java.lang.NumberFormatException: 
04-30 14:37:10.073: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
04-30 14:37:10.073: E/AndroidRuntime(273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
04-30 14:37:10.073: E/AndroidRuntime(273): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
04-30 14:37:10.073: E/AndroidRuntime(273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
04-30 14:37:10.073: E/AndroidRuntime(273): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-30 14:37:10.073: E/AndroidRuntime(273): at android.os.Looper.loop(Looper.java:123) 
04-30 14:37:10.073: E/AndroidRuntime(273): at android.app.ActivityThread.main(ActivityThread.java:4627) 
04-30 14:37:10.073: E/AndroidRuntime(273): at java.lang.reflect.Method.invokeNative(Native Method) 
04-30 14:37:10.073: E/AndroidRuntime(273): at java.lang.reflect.Method.invoke(Method.java:521) 
04-30 14:37:10.073: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
04-30 14:37:10.073: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
04-30 14:37:10.073: E/AndroidRuntime(273): at dalvik.system.NativeStart.main(Native Method) 
04-30 14:37:10.073: E/AndroidRuntime(273): Caused by: java.lang.NumberFormatException: 
04-30 14:37:10.073: E/AndroidRuntime(273): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267) 
04-30 14:37:10.073: E/AndroidRuntime(273): at java.lang.Double.parseDouble(Double.java:287) 
04-30 14:37:10.073: E/AndroidRuntime(273): at com.era.restaurant.versionoct.menu.ProductInformationActivity.onCreate(ProductInformationActivity.java:77) 
04-30 14:37:10.073: E/AndroidRuntime(273): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-30 14:37:10.073: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
04-30 14:37:10.073: E/AndroidRuntime(273): ... 11 more 
+0

什麼是你的行數77'04-30 14:37:10.073:E/AndroidRuntime(273):at com.era.restaurant.versionoct.menu.ProductInformationActivity.onCreate(ProductInformationActivity.java:77)' – SilentKiller 2013-04-30 09:26:03

+0

首先查看ProductInformationActivity.java行77 – 2013-04-30 09:26:05

+0

可能是由於文本應該爲null或txt_total.setText(String.valueOf(itemamount)); – 2013-04-30 09:30:03

回答

1

你試圖解析字符串轉化成一個Double。字符串來自:

text_cost_code.getText() 

因此,如果text_cost_code不是一個數字,你會得到這個異常。確保textView保存正確的格式化數字。

+0

我發佈了更多的ProductInformationActivity.java代碼,現在檢查 – 2013-04-30 09:33:16

+0

您需要圍繞try/catch中的語句。查看ling的答案。 – edoardotognoni 2013-04-30 09:40:27

1

Double.parseDouble(text_cost_code.getText().toString());將拋出一個NumberFormatException,因爲Double.parseDouble()無法處理不符合其預期格式的字符串。檢查text_cost_code.getText()是否包含一個數字。

+0

我張貼ProductInformationActivity.java的更多的代碼,檢查現在 – 2013-04-30 09:34:38

0

使用此:

try { 
itemamount = Double.parseDouble(text_cost_code.getText().toString()); // Same 
} catch (NumberFormatException e) { 
    itemamount=0.0; 
// Toast.makeText(getBaseContext(), "Error: Edittext must contain numbers " ,Toast.LENGTH_SHORT).show(); 
} 
txt_total.setText(String.valueOf(itemamount)); 

根據您的編輯

if (!edit_qty_code.getText().toString().equals("") 
       || !edit_qty_code.getText().toString().equals("")) 

這裏,dit_qty_code你只檢查是否爲空或沒有,你沒有檢查它是否包含數字

+0

我張貼ProductInformationActivity.java的更多的代碼,現在 – 2013-04-30 09:32:32

+0

查見我的編輯答案,查看代碼仔細txt_total.setText(將String.valueOf(itemamount)); – 2013-04-30 09:37:13

0

使用驗證之前TypeCasting直接從TextView或EditText得到的值

if (text_cost_code.getText().toString().length() > 0) { 
     itemamount = Double.parseDouble(text_cost_code.getText().toString()); 
    } else { 
     itemamount = 0; 
    } 

在此之前,你必須保持android:inputtype="number"驗證的XML代碼本的EditText所以用戶不會允許除numericals加... :)

+0

如果text_cost_code包含「hai」,則會發生什麼 – 2013-04-30 09:44:51

+0

@ling。如果EditText被命名爲成本代碼,所以成本必須有'inputType作爲數字',所以它不會允許字母.. – SilentKiller 2013-04-30 09:49:28

+0

告訴Chulbul Pandey – 2013-04-30 09:50:49

相關問題