これが私の方法です
private TextView monthTotal;
private TextView yearTotal;
private ExpenseDal expenseDal;
private EditText amountText;
private EditText descText;
private EditText categoryText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
expenseDal = new ExpenseDal(this);
amountText = (EditText) findViewById(R.id.amountText);
descText = (EditText) findViewById(R.id.descText);
categoryText = (EditText) findViewById(R.id.categoryText);
setContentView(R.layout.activity_expense_list);
monthTotal = (TextView) findViewById(R.id.monthTotal);
yearTotal = (TextView) findViewById(R.id.yearTotal);
monthTotal.setText(expenseDal.getCurrentMonthTotal());
yearTotal.setText(expenseDal.getCurrentYearTotal());
//Event For Save Button
Button saveButton = (Button) findViewById(R.id.saveBtn);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (amountText.getText().length() != 0) {
AsyncTask<Object, Object, Object> saveExpenseTask = new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... params) {
saveExpense();
return null;
}
@Override
protected void onPostExecute(Object result) {
finish();
}
};
saveExpenseTask.execute((Object[]) null);
}
else {
AlertDialog.Builder alert = new AlertDialog.Builder(
ExpenseList.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});
}
private void saveExpense() { }
以下の私のトレース
12-17 04:53:53.003: D/AndroidRuntime(2484): Shutting down VM
12-17 04:53:53.003: W/dalvikvm(2484): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-17 04:53:53.003: E/AndroidRuntime(2484): FATAL EXCEPTION: main
12-17 04:53:53.003: E/AndroidRuntime(2484): java.lang.NullPointerException
12-17 04:53:53.003: E/AndroidRuntime(2484): at com.technowizsol.easyexpensemanager.ExpenseList$1.onClick(ExpenseList.java:45)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.view.View.performClick(View.java:4084)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.view.View$PerformClick.run(View.java:16966)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.os.Handler.handleCallback(Handler.java:615)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.os.Looper.loop(Looper.java:137)
12-17 04:53:53.003: E/AndroidRuntime(2484): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-17 04:53:53.003: E/AndroidRuntime(2484): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 04:53:53.003: E/AndroidRuntime(2484): at java.lang.reflect.Method.invoke(Method.java:511)
12-17 04:53:53.003: E/AndroidRuntime(2484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-17 04:53:53.003: E/AndroidRuntime(2484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-17 04:53:53.003: E/AndroidRuntime(2484): at dalvik.system.NativeStart.main(Native Method)
エラーは、金額がnullかどうかを確認する行にあります。なぜ私がこれを手に入れるのか、どんな考えでも。