This is my Main Activity. I've used Log.d to get it down to where it stops running, which is at onCreate4. I'm not sure why it stops in the getReferences section. I run the program in the emulator, but it tells me automatically that my program has stopped working.
package com.example.carloanbuddy4;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.widget.TextView.OnEditorActionListener;
public class MainActivity extends Activity implements OnClickListener,
OnEditorActionListener, OnItemSelectedListener, OnFocusChangeListener,
OnCheckedChangeListener {
private TextView payment;
private TextView interest;
private EditText principle;
private TextView interestText;
private CheckBox interestBox;
private EditText years;
private TextView apr;
Button plusbutton;
Button minusbutton;
private static String TAG = "CAR";
private Spinner period;
private double aprPercent;
private int t;
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate1");
this.getReferences();
this.setListeners();
}
@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);
}
public void getReferences() {
payment = (TextView) findViewById(R.id.paymentLabel);
Log.d(TAG, "onCreate2");
interest = (TextView) findViewById(R.id.interestLabel);
Log.d(TAG, "onCreate3");
apr = (TextView) findViewById(R.id.aprLabel);
Log.d(TAG, "onCreate4");
aprPercent = Double.parseDouble(apr.getText().toString());
Log.d(TAG, "onCreate5");
interestText = (TextView) findViewById(R.id.interestText);
Log.d(TAG, "onCreate6");
interestBox = (CheckBox) findViewById(R.id.checkBox1);
Log.d(TAG, "onCreate7");
interestBox.setChecked(false);
principle = (EditText) findViewById(R.id.principleEditText);
Log.d(TAG, "onCreate8");
years = (EditText) findViewById(R.id.yearsEditText);
Log.d(TAG, "onCreate9");
period = (Spinner) findViewById(R.id.spinner1);
Log.d(TAG, "onCreate10");
minusbutton = (Button) findViewById(R.id.minusbutton);
Log.d(TAG, "onCreate11");
plusbutton = (Button) findViewById(R.id.plusbutton);
Log.d(TAG, "onCreate12");
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.options,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
period.setAdapter(adapter);
Log.d(TAG, "onCreate13");
// principle.setOnFocusChangeListener(this);
Log.d(TAG, "getReferences principle: " + principle.getText()
+ " years:" + years.getText());
}
public void setListeners() { // where the event being consumed will be
// responding
principle.setOnFocusChangeListener(this);
principle.setOnEditorActionListener(this);
years.setOnFocusChangeListener(this);
years.setOnEditorActionListener(this);
interestBox.setOnCheckedChangeListener(this);
period.setOnItemSelectedListener(this);
Log.d(TAG, "setListeners principle: " + principle.getText() + " years:"
+ years.getText());
}
public void setPeriodValue() {
if (period.getSelectedItem().toString().equalsIgnoreCase("Monthly")) {
t = 12;
} else if (period.getSelectedItem().toString()
.equalsIgnoreCase("Quarterly")) {
t = 4;
} else if (period.getSelectedItem().toString()
.equalsIgnoreCase("Annually")) {
t = 1;
}
}
public void updateResults() {
double dblPrinciple = Double
.parseDouble(principle.getText().toString());
double dblYears = Double.parseDouble(years.getText().toString());
double num, denom, dblPayment;
double r = aprPercent / 100;
NumberFormat nf = NumberFormat.getNumberInstance();
NumberFormat curr = NumberFormat.getCurrencyInstance();
apr.setText(nf.format(aprPercent));
setPeriodValue();
num = (r / t);
denom = (1 - Math.pow((1 + num), (t * -dblYears)));
dblPayment = dblPrinciple * (num / denom);
payment.setText(curr.format(dblPayment));
interest.setText(curr
.format((dblPayment * t * dblYears) - dblPrinciple));
}
public void onFocusChange(View v, boolean hasfocus) {
Log.d(TAG, "Focus Change principle: " + principle.getText() + " years"
+ years.getText());
switch (v.getId()) {
case R.id.principleEditText:
case R.id.yearsEditText:
updateResults();
default:
updateResults();
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(interestBox.isChecked()) {
interest.setVisibility(View.VISIBLE);
interestText.setVisibility(View.VISIBLE);
}
else {
interest.setVisibility(View.INVISIBLE);
interestText.setVisibility(View.INVISIBLE);
}
if (interestBox.isChecked()!=true){
interest.setVisibility(View.INVISIBLE);
interestText.setVisibility(View.INVISIBLE);
}
else {
interest.setVisibility(View.VISIBLE);
interestText.setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
updateResults();
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
updateResults();
return false;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.minusbutton:
if (aprPercent == 1) {
break;
} else {
aprPercent = aprPercent - 1.0;
updateResults();
break;
}
case R.id.plusbutton:
if (aprPercent == 20) {
break;
} else {
aprPercent = aprPercent + 1.0;
updateResults();
break;
}
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
updateResults();
}
}
This is my XML file:
<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.carloanbuddy4.MainActivity" >
<TableLayout
android:id="@+id/mainTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0, 1"
android:stretchColumns="0, 1">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/principleText"
android:ems="5"
android:textStyle="bold" />
<EditText
android:id="@+id/principleEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:ems="10"
android:inputType="numberDecimal"
android:text="@string/principle_entered"
android:textSize="12sp" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/yearsText"
android:ems="5"
android:textStyle="bold" />
<EditText
android:id="@+id/yearsEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:ems="5"
android:inputType="number"
android:labelFor="@id/yearsEditText"
android:text="@string/years_entered"
android:textSize="12sp" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/aprText"
android:ems="5"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/aprLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:ems="5"
android:labelFor="@id/aprLabel"
android:paddingLeft="12dp"
android:textSize="20sp" >
</TextView>
<Button
android:id="@+id/minusbutton"
android:onClick="onClick"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/minus_button" />
<Button
android:id="@+id/plusbutton"
android:onClick="onClick"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plus_button" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/periodText"
android:textSize="12sp"
android:textStyle="bold" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:checked="false"
android:focusableInTouchMode="false"
android:text="@string/interest_checkbox"
android:textSize="12sp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<TextView
android:id="@+id/paymentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/paymentText"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/paymentLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/payment_total"
android:textSize="12sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/tableRow8"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/interestText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/interestText"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/interestLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/interest_total"
android:textSize="12sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/tableRow9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp" >
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:checked="true"
android:focusableInTouchMode="false"
android:text="@string/save_checkbox"
android:textSize="12sp" />
</TableRow>
</TableLayout>
</RelativeLayout>