2 回答
TA贡献1812条经验 获得超5个赞
这是因为在向意图添加扩展数据时您有不同的密钥。您在方法中使用以下"Loan.loanAmount"键calculatePayment:
intent.putExtra("Loan.loanAmount", Double.valueOf(amount.toString()));
但你期望"loanAmount"在paymentViewActivity课堂上:
public class paymentViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...
double amount = intent.getDoubleExtra("loanAmount", loanAmount);
}
请注意该Intent.putExtra(String, Value)参数使用键值格式。字符串是特定值的键标识符。
因此,为您的 putExtra 使用相同的密钥。更安全的方法是在相关活动中使用常量字符串作为键。例如,LOAN_AMOUNT_KEY为您的贷款金额值使用常量字符串:
public class paymentViewActivity extends AppCompatActivity {
public static final String LOAN_AMOUNT_KEY = "loanAmountKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// use the key to find the value
// you won't use the incorrect key because
// the key is tightly coupled to the activity
double amount = intent.getDoubleExtra(LOAN_AMOUNT_KEY, loanAmount);
}
}
然后,在您的calculatePayment方法中,在保存值时使用密钥:
public void calculatePayment(View view) {
Intent intent = new Intent(this, paymentViewActivity.class);
...
double amount = Double.valueOf(amount.toString());
intent.putExtra(paymentViewActivity.LOAN_AMOUNT_KEY, amount);
...
}
笔记:
为您的活动使用正确的 java 命名约定类名。例如,
PaymentViewActivity
改为使用paymentViewActivity
避免
public static
用于您的模型成员类。你最好使用getter/setter。
TA贡献1842条经验 获得超12个赞
intent.putExtra(String.valueOf(Loan.interestRate), interest); --> wrong
intent.putExtra(String.valueOf(Loan.loanDuration), radio_b.getText().toString());
您只需要添加字符串名称,然后您才能从附加组件中获取它。
intent.putExtra("interest", interest);
intent.putExtra("loanduration", radio_b.getText().toString());
添加回答
举报