I am trying to develop a very simple App that calculates the total interest paid for a home mortgage. There are 2 screens. The first one has the user input their monthly payment, the number of years for their loan and the initial loan amount. The first screen loads just fine. But when the button is clicked I get the good old "Unfortunately "app" has stopped working. Wondering if anyone could shed any light as to why. Here is the java code for that screen:
package com.example.mortgage;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
package com.example.mortgage;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
int enterPayment;
int enterYears;
int enterPrincipal;
int totalInterest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText payment=(EditText)findViewById(R.id.txtPayment);
final EditText years=(EditText)findViewById(R.id.txtYear);
final EditText principal=(EditText)findViewById(R.id.txtPrincipal);
Button btCompute = (Button)findViewById(R.id.btnCompute);
final SharedPreferences sharedPref=PreferenceManager.getDefaultSharedPreferences(this);
btCompute.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
enterPayment = Integer.parseInt(payment.getText().toString());
enterYears = Integer.parseInt(years.getText().toString());
enterPrincipal = Integer.parseInt(principal.getText().toString());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("key1", enterPayment);
editor.putInt("key2", enterYears);
editor.putInt("key3", enterPrincipal);
editor.commit();
startActivity(new Intent(MainActivity.this, Status.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And the XML file for that screen:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mortgage.MainActivity" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/txtTitle"
android:textSize="30sp" />
<EditText
android:id="@+id/txtPayment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:ems="10"
android:inputType="number"
android:hint="@string/hint1" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/txtYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtPayment"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:ems="10"
android:inputType="number"
android:hint="@string/hint2" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/txtPrincipal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtYear"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:ems="10"
android:inputType="number"
android:hint="@string/hint3" >
<requestFocus />
</EditText>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="177dp"
android:src="@drawable/mortgage"
android:contentDescription="@string/imgMortgage" />
<Button
android:id="@+id/btnCompute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:text="@string/btnCompute" />
</RelativeLayout>
The next screen shows the calculated interest.
package com.example.mortgage;
public class Status extends Activity {
int months;
int totalInterest;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
TextView result = (TextView)findViewById(R.id.txtTotal);
ImageView image = (ImageView)findViewById(R.id.imgYearLoan);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int intPayment = sharedPref .getInt("key1", 0);
int intYears = sharedPref.getInt("key2", 0);
int intLoan = sharedPref.getInt("key3", 0);
if(intYears == 10){
months = 120;
totalInterest = (intPayment * months) - intLoan;
}else if(intYears == 20){
months = 240;
totalInterest = (intPayment * months) - intLoan;
}else {
months = 360;
totalInterest = (intPayment * months) - intLoan;
}
result.setText(totalInterest);
image.setImageResource(R.drawable.ten);
}
}
And the XML for that screen:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/txtTitle" />
<TextView
android:id="@+id/txtTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:ems="10"
android:hint="@string/hint4" />
<ImageView
android:id="@+id/imgYearLoan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="95dp"
android:contentDescription="@string/imgYearLoan" />
</RelativeLayout>
0 comments:
Post a Comment