我有一个 ListView,每行都有一个 EditText。用户可以更新 EditText 中填充的值,然后单击按钮以刷新 ListView 顶部的新总和。我这里的方法是遍历所有 ListView 记录并获取 EditText 的值,执行必要的计算,然后更新 UI。我的问题是我在第五次迭代中不断收到 NullPointerException,即使肯定填充了一个值。它在前四次迭代中运行良好。我的代码是否正确和/或我应该对我正在尝试做的事情采取不同的方法:
button_finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double sumOfAppliedAmounts = 0;
Bundle extras = getIntent().getExtras();
double exchange_rate = Double.parseDouble(extras.getString("exchange_rate")) / 100;
double convertedPaymentAmount;
if (exchange_rate >= 1)
convertedPaymentAmount = Double.parseDouble(extras.getString("payment_amount")) / exchange_rate;
else
convertedPaymentAmount = Double.parseDouble(extras.getString("payment_amount")) * exchange_rate;
for (int i = 0; i < (m_listview.getCount()); i++) {
v = m_listview.getChildAt(i);
applied_amount = (EditText) v.findViewById(R.id.list_text_applied_amount_value);
sumOfAppliedAmounts = sumOfAppliedAmounts + Double.parseDouble(applied_amount.getText().toString());
}
text_remaining_converted_amount.setText(String.valueOf(convertedPaymentAmount - sumOfAppliedAmounts));
}
});