主要活动不为变量赋值

在我的作业问题中,我应该根据用户提供的贷款金额利率和贷款期限来计算每月的贷款支付。这是使用意图来完成的。


当我运行应用程序时,变量的值永远不会从 0 改变,然后它会转到应用程序的第二页,应该显示结果,但它说 NaN,应该显示结果。


我对 Android 开发非常陌生,我不知道问题出在哪里,所以我发布了我拥有的所有主要 Java 代码。


名为Loan.java 的类


package com.example.collegeloanpayoffcalculator;


public class Loan

{


    public static double loanAmount = 0.0;

    public static double interestRate = 0.0;

    public static int loanDuration = 0;

    //public static double monthlyPayment = 0.0;

    public double amount;

    public double interest;

    public double duration;

    //public double payment;


    public Loan() {}


    public Loan(double amount, double interest, double duration)

    {

        this.amount = amount;

        this.interest = interest;

        this.duration = duration;

    }

}

名为MainActivity.java的类


   public void calculatePayment(View view)

    {

        Intent intent = new Intent(this, paymentViewActivity.class);

        EditText loanAmount = (EditText) findViewById(R.id.loanAmount);

        Editable amount = loanAmount.getText();

        intent.putExtra("Loan.loanAmount", Double.valueOf(amount.toString()));


        EditText interestRate = (EditText) findViewById(R.id.interestRateEditText);

        Editable interest = interestRate.getText();

        intent.putExtra(String.valueOf(Loan.interestRate), interest);


        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.duration);

        int selected_id = radioGroup.getCheckedRadioButtonId();

        RadioButton radio_b = (RadioButton) findViewById(selected_id);


        intent.putExtra(String.valueOf(Loan.loanDuration), radio_b.getText().toString());

        startActivity(intent);

    }

}


临摹微笑
浏览 84回答 2
2回答

慕雪6442864

这是因为在向意图添加扩展数据时您有不同的密钥。您在方法中使用以下"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。

红颜莎娜

intent.putExtra(String.valueOf(Loan.interestRate), interest); --> wrongintent.putExtra(String.valueOf(Loan.loanDuration), radio_b.getText().toString());您只需要添加字符串名称,然后您才能从附加组件中获取它。intent.putExtra("interest", interest);intent.putExtra("loanduration", radio_b.getText().toString());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java