基本上我有一个包含多个 TableRow 的 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") 工作正常,调试器也会将“价格”分类为字符串。所以我必须承认我不知道为什么这个错误不断发生。
如果有更好、更不容易出错的方法来获取特定行的单元格,请告诉我)
最佳答案
您需要使用 Integer.valueOf()而不是 Integer.getInteger() .如果您查看 Integer.getInteger() 的 API 文档,您会看到它“返回由给定字符串标识的系统属性的整数值”。方法名称有点误导。
关于android - 读取 TableRow 单元格值并返回整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9915877/