2012-03-28 29 views
1

基本上我有一個TableLayout與幾個TableRows,每個包含5個單元格(Name,Price,Count,IncreaseButton,DecreaseButton),然後我寫了一個函數來返回一個某些行,但我不斷收到異常。讀取TableRow單元格值並返回一個整數

這是代碼:

private int getRowPrice(View target) //target is a Button 
{ 
    TableRow row = (TableRow) target.getParent(); 
    TextView priceCell = (TextView) row.getChildAt(1); 

    String price = priceCell.getText().toString(); 

    //The cell text may start with $ or another currency 
    if (price.startsWith(getString(R.string.currency))) 
    { 
     price = price.substring(getString(R.string.currency).length()).toString(); 
    } 

    return Integer.getInteger(price); //Here I get a "java.lang.NullPointerException" 
} 

如果我Log.d()它輸出正確的值,如果我嘗試返回正常工作getInteger(「10」),也調試器價格將'價格'分類爲一個字符串。所以我必須承認,我不知道爲什麼這個錯誤會持續發生。

並請讓我知道,如果有更好的,錯誤傾向更低的方式來獲得一定行的單元格)

回答

1

您需要使用Integer.valueOf()而不是Integer.getInteger()。如果您查看Integer.getInteger()的API文檔,您會看到它「返回由給定字符串標識的系統屬性的整數值」。方法名稱有點誤導。

相關問題