アプリでホームボタンを押すと、アプリがクラッシュします。これはLogCatです。誰かがこの問題を解決するのを手伝ってくれますか?
08-22 14:28:36.078: E/AndroidRuntime(509): FATAL EXCEPTION: main
08-22 14:28:36.078: E/AndroidRuntime(509): java.lang.RuntimeException: Unable to pause activity {com.example.girtsreminder/com.example.girtsreminder.ReminderEditActivity}: java.lang.NullPointerException
08-22 14:28:36.078: E/AndroidRuntime(509): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
08-22 14:28:36.078: E/AndroidRuntime(509): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
08-22 14:28:36.078: E/AndroidRuntime(509): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
08-22 14:28:36.078: E/AndroidRuntime(509): at android.app.ActivityThread.access$2500(ActivityThread.java:125)
08-22 14:28:36.078: E/AndroidRuntime(509): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
08-22 14:28:36.078: E/AndroidRuntime(509): at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 14:28:36.078: E/AndroidRuntime(509): at android.os.Looper.loop(Looper.java:123)
08-22 14:28:36.078: E/AndroidRuntime(509): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-22 14:28:36.078: E/AndroidRuntime(509): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 14:28:36.078: E/AndroidRuntime(509): at java.lang.reflect.Method.invoke(Method.java:521)
08-22 14:28:36.078: E/AndroidRuntime(509): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-22 14:28:36.078: E/AndroidRuntime(509): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-22 14:28:36.078: E/AndroidRuntime(509): at dalvik.system.NativeStart.main(Native Method)
08-22 14:28:36.078: E/AndroidRuntime(509): Caused by: java.lang.NullPointerException
08-22 14:28:36.078: E/AndroidRuntime(509): at com.example.girtsreminder.ReminderEditActivity.onSaveInstanceState(ReminderEditActivity.java:273)
08-22 14:28:36.078: E/AndroidRuntime(509): at android.app.Activity.performSaveInstanceState(Activity.java:1036)
08-22 14:28:36.078: E/AndroidRuntime(509): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
08-22 14:28:36.078: E/AndroidRuntime(509): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3330)
アップデート:
リマインダー編集アクティビティ:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
public class ReminderEditActivity extends SherlockActivity {
//
// Dialog Constants
//
private static final int DATE_PICKER_DIALOG = 0;
private static final int TIME_PICKER_DIALOG = 1;
//
// Date Format
//
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String TIME_FORMAT = "kk:mm";
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd kk:mm:ss";
private EditText mTitleText;
private EditText mBodyText;
private Button mDateButton;
private Button mTimeButton;
private Button mConfirmButton;
private Long mRowId;
private RemindersDbAdapter mDbHelper;
private Calendar mCalendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new RemindersDbAdapter(this);
setContentView(R.layout.reminder_edit);
mCalendar = Calendar.getInstance();
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
mDateButton = (Button) findViewById(R.id.reminder_date);
mTimeButton = (Button) findViewById(R.id.reminder_time);
mRowId = savedInstanceState != null ? savedInstanceState.getLong(RemindersDbAdapter.KEY_ROWID)
: null;
registerButtonListenersAndSetDefaultText();
com.actionbarsherlock.app.ActionBar aBar = getSupportActionBar();
String dateString = (String) android.text.format.DateFormat.format("MMM d, yyyy", new java.util.Date());
aBar.setTitle(dateString);
}
private void setRowIdFromIntent() {
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(RemindersDbAdapter.KEY_ROWID)
: null;
}
}
@Override
protected void onPause() {
super.onPause();
mDbHelper.close();
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// TODO Auto-generated method stub
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.edit, (com.actionbarsherlock.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemSelected(int featureId,
com.actionbarsherlock.view.MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()) {
case R.id.accept:
saveState();
setResult(RESULT_OK);
Toast.makeText(ReminderEditActivity.this, getString(R.string.task_saved_message), Toast.LENGTH_SHORT).show();
finish();
return true;
case R.id.back:
finish();
// Intent i = new Intent(this, ReminderListActivity.class);
//startActivity(i);
return super.onMenuItemSelected(featureId, item);}
return true;
}
@Override
protected void onResume() {
super.onResume();
mDbHelper.open();
setRowIdFromIntent();
populateFields();
}
@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case DATE_PICKER_DIALOG:
return showDatePicker();
case TIME_PICKER_DIALOG:
return showTimePicker();
}
return super.onCreateDialog(id);
}
private DatePickerDialog showDatePicker() {
DatePickerDialog datePicker = new DatePickerDialog(ReminderEditActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, monthOfYear);
mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDateButtonText();
}
}, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
return datePicker;
}
private TimePickerDialog showTimePicker() {
TimePickerDialog timePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, minute);
updateTimeButtonText();
}
}, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), true);
return timePicker;
}
private void registerButtonListenersAndSetDefaultText() {
mDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_PICKER_DIALOG);
}
});
mTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_PICKER_DIALOG);
}
});
updateDateButtonText();
updateTimeButtonText();
}
private void populateFields() {
// Only populate the text boxes and change the calendar date
// if the row is not null from the database.
if (mRowId != null) {
Cursor reminder = mDbHelper.fetchReminder(mRowId);
startManagingCursor(reminder);
mTitleText.setText(reminder.getString(
reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_TITLE)));
mBodyText.setText(reminder.getString(
reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_BODY)));
// Get the date from the database and format it for our use.
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
Date date = null;
try {
String dateString = reminder.getString(reminder.getColumnIndexOrThrow(RemindersDbAdapter.KEY_DATE_TIME));
date = dateTimeFormat.parse(dateString);
mCalendar.setTime(date);
} catch (ParseException e) {
Log.e("ReminderEditActivity", e.getMessage(), e);
}
} else {
// This is a new task - add defaults from preferences if set.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String defaultTitleKey = getString(R.string.pref_task_title_key);
String defaultTimeKey = getString(R.string.pref_default_time_from_now_key);
String defaultTitle = prefs.getString(defaultTitleKey, null);
String defaultTime = prefs.getString(defaultTimeKey, null);
if(defaultTitle != null)
mTitleText.setText(defaultTitle);
if(defaultTime != null)
mCalendar.add(Calendar.MINUTE, Integer.parseInt(defaultTime));
}
updateDateButtonText();
updateTimeButtonText();
}
private void updateTimeButtonText() {
// Set the time button text based upon the value from the database
SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);
String timeForButton = timeFormat.format(mCalendar.getTime());
mTimeButton.setText(timeForButton);
}
private void updateDateButtonText() {
// Set the date button text based upon the value from the database
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
String dateForButton = dateFormat.format(mCalendar.getTime());
mDateButton.setText(dateForButton);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(RemindersDbAdapter.KEY_ROWID, mRowId);
}
private void saveState() {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT);
String reminderDateTime = dateTimeFormat.format(mCalendar.getTime());
if (mRowId == null) {
long id = mDbHelper.createReminder(title, body, reminderDateTime);
if (id > 0) {
mRowId = id;
}
} else {
mDbHelper.updateReminder(mRowId, title, body, reminderDateTime);
}
new ReminderManager(this).setReminder(mRowId, mCalendar);
}
}
質問を更新しました。これは、エラーが発生するアクティビティです。誰かが間違いを見つけることを願っています...