I am programming a mortgage calculator app. I thought I have finished the app and it should work now. However every time I click the button "Calculate Monthly Payment", the app stops and exit immediately with the error message "Unfortunately, your app has stopped".
Here is my code fragment for the MainActivity.java:
public class MainActivity extends ActionBarActivity
{
Button button;
EditText borrowedAmt;
SeekBar interestRate;
RadioGroup loanTerm;
RadioButton termChosen;
CheckBox taxAndInsur;
TextView monthlyPay;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
borrowedAmt = (EditText) findViewById(R.id.editText1);
loanTerm = (RadioGroup) findViewById(R.id.radioGroup1);
monthlyPay = (TextView) findViewById(R.id.textView4);
taxAndInsur = (CheckBox) findViewById(R.id.checkBox1);
interestRate = (SeekBar) findViewById(R.id.seekBar1);
button = (Button) findViewById(R.id.button1);
// SeekBar
interestRate.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
int progressChanged = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
progressChanged = progress;
}
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar)
{
Toast.makeText(MainActivity.this,"Rate chosen = " + progressChanged,
Toast.LENGTH_SHORT).show();
}
});
//Button
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
double borrowedAmtValue = 0.0;
double rateValue = 0.0;
int noOfLoanMonth = 0;
double taxAndInsurValue = 0.0;
double monthlyPayValue = 0.0;
// Borrowed amount processing
borrowedAmtValue = Double.parseDouble(borrowedAmt.getText().toString());
// Interest Rate processing
rateValue = interestRate.getProgress() / 1200;
// Loan Term processing
int[] termGroup = {180, 240, 360};
int selectedTerm = loanTerm.getCheckedRadioButtonId();
noOfLoanMonth = termGroup[selectedTerm];
// Tax and Insurance Included processing
if (taxAndInsur.isChecked())
taxAndInsurValue = borrowedAmtValue * 0.001;
else
taxAndInsurValue = 0.0;
//making the keyboard disappear after clicking the button
InputMethodManager objInputMM = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
objInputMM.hideSoftInputFromWindow(termChosen.getWindowToken(), 0);
//Calculating total interest value
monthlyPayValue = borrowedAmtValue * (rateValue / (1 - Math.pow(1 + rateValue, - noOfLoanMonth)));
// setting the result into the textview's text property to display to the user
monthlyPay.setText(Double.toString(monthlyPayValue));
//txtVwCalculatedInterest.setBackgroundColor(Color.parseColor("#FFCDEF"));
// resetting text boxes
//loanAmt.setText("");
//loanTenure.setText("");
}
});
}
Here is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/enter_borrowed_amt"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/choose_int_rate"
android:textAppearance="?android:attr/textAppearanceMedium" />
<SeekBar
android:id="@+id/seekBar1"
android:progress="10"
android:max="20"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/choose_term"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="clip_vertical"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="@string/yr15" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/yr20" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/yr30" />
</RadioGroup>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/check_tni" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/calculate"
android:onClick="setOnClickListener"/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/monthly_pay"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp" />
</LinearLayout>
When I run the app on emulator, I realize when I click the button, that the logcat window brings up tons of red lines, so I think there is something wrong here but I don't know how to detect it. Please help me. Thanks much!
My catlog: https://www.dropbox.com/s/ab0m7y05dzzsc9u/Untitled.jpg?dl=0