2012-03-28 63 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文档,您会看到它“返回由给定字符串标识的系统属性的整数值”。方法名称有点误导。

相关问题