读取TableRow单元格值并返回整数

时间:2012-03-28 21:25:06

标签: android integer tablelayout tablerow

基本上我有一个带有几个TableRows的TableLayout,每个包含5个单元格(Name,Price,Count,IncreaseButton,DecreaseButton),然后我写了一个函数来返回某一行的价格,但我一直得到一个例外

这是代码:

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”),调试器也将'price'分类为字符串。所以我必须承认我不知道为什么会出现这种错误。

请告诉我是否有更好的,更少出错的方法来获取特定行的单元格)

1 个答案:

答案 0 :(得分:1)

您需要使用Integer.valueOf()代替Integer.getInteger()。如果查看Integer.getInteger()的API文档,您将看到它“返回由给定字符串标识的系统属性的Integer值”。方法名称有点误导。