【问题标题】:Reading a TableRow cell value and returning an integer读取 TableRow 单元格值并返回一个整数
【发布时间】:2012-03-28 21:25:05
【问题描述】:

基本上我有一个包含几个 TableRows 的 TableLayout,每个包含 5 个单元格(名称、价格、计数、增加按钮、减少按钮),然后我编写了一个函数来返回某一行的价格,但我不断收到异常.

这是代码:

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"),调试器也将“价格”分类为字符串。所以我必须承认我不知道为什么这个错误不断发生。

如果有更好、更不容易出错的方法来获取某一行的单元格,请告诉我)

【问题讨论】:

    标签: android integer tablelayout tablerow


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多