我最近正在构建一个应用程序,使用Java计算抵押贷款作为我在Android工作室的学校项目。但是,当我尝试在我的平板电脑上运行应用程序时,它会一直崩溃并显示“不幸的是应用已经停止”。我想知道我的代码有什么问题,下面是我的代码。 这是我的控制者:
package ca.roumani.mcalc;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import hr.YumModel;
public class EntryForum extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry_forum);
}
public void buttonClicked(View v)
{
View principleView = findViewById(R.id.principlebox);
EditText principleEdit = (EditText) principleView;
String principle = principleEdit.getText().toString();
View amortizationView = findViewById(R.id.amortizationbox);
EditText amortizationEdit = (EditText) amortizationView;
String amortization = amortizationEdit.getText().toString();
View annualinterestView = findViewById(R.id.interestbox);
EditText annualinterestEdit = (EditText) annualinterestView;
String annualinterest = annualinterestEdit.getText().toString();
MortgageModel model = new MortgageModel(principle, amortization, annualinterest);
String answer = model.computePayment();
((TextView) findViewById(R.id.answer)).setText( "$" + answer);
}
public void yumButton(View v)
{
View principleView = findViewById(R.id.principlebox);
EditText principleEdit = (EditText) principleView;
String principle = principleEdit.getText().toString();
View amortizationView = findViewById(R.id.amortizationbox);
EditText amortizationEdit = (EditText) amortizationView;
String amortization = amortizationEdit.getText().toString();
View annualinterestView = findViewById(R.id.interestbox);
EditText annualinterestEdit = (EditText) annualinterestView;
String annualinterest = annualinterestEdit.getText().toString();
YumModel yumModel = new YumModel();
yumModel.setAmortization(amortization);
yumModel.setInterest(annualinterest);
yumModel.setPrinciple(principle);
String answer2 = yumModel.computePayment();
((TextView) findViewById(R.id.answer2)).setText(answer2);
}
}
以下是我的模特:
public class MortgageModel
{
private double principle;
private int amortization;
private double annualInterest;
public MortgageModel(String p, String a, String i)
{
this.principle = Double.parseDouble(p);
this.amortization = Integer.parseInt(a);
this.annualInterest = Double.parseDouble(i);
}
public String computePayment()
{
double r = this.annualInterest * 0.01 / 12 ;
double n = this.amortization * 12;
double index1 = 1 + (n * r) + (n * (n-1) * r * r) / 2;
double index2 = 1 - (1 / index1);
double index3 = (r * this.principle) / index2;
String result = String.format("%.d", index3);
return result;
}
}
有人知道我的代码有什么问题吗?