这是我的主要活动。我已经使用Log.d将它下载到它停止运行的位置,即onCreate4。我不确定为什么它会在getReferences部分停止。我在模拟器中运行该程序,但它会自动告诉我我的程序已停止工作。
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();
}
}
这是我的XML文件:
<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>
答案 0 :(得分:0)
它认为您在调用apr.getText()。toString()之前,但在我看到TextFile有任何文本之前,XML文件和java代码都没有调用,所以我猜它会返回null然后你有一个NullPointException。你可以检查一下。
如果没有,试试这个:
调用getReferences()方法时,添加一个try-catch块,以便你的onCreate方法如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate1");
try {
this.getReferences();
} catch (Exception e){
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
Log.i("Exception", errors.toString());
}
this.setListeners();
}
然后,在LogCat窗口中,单击绿色十字按钮(+)。在“过滤器名称”字段中,使用您希望的名称,在&#34;中使用日志标记&#34;输入Exception并按日志级别选择信息。
这样您就可以过滤所有消息,并且只保留堆栈跟踪。检查正在抛出的异常,如果您没有找到解决方案,您可以随时返回此处并使用您刚刚实现的新信息更新问题。
我希望它有所帮助。