Reputation:
I am somewhat new to Android and am trying to develop my first production app. It is a user defined data storage app that consists of a list activity and an add dialog activity as well as a db adapter. All my "CRUD" (create, read, update, delete) functions seem to work perfectly. But I am running into the following error when I change the orientation of the add dialog when the data has not been saved to the db. In other words, my onSavedInstanceState() function is failing when I change the orientation from portrait to landscape or vice versa.
02-16 11:42:05.321: E/AndroidRuntime(347): java.lang.RuntimeException: Unable to pause activity {net.learn2develop.Homework/net.learn2develop.Homework.HomeworkEditActivity}: java.lang.NullPointerException
here is my code:
public class HomeworkEditActivity extends Activity{
private Long mRowId;
// SQLite Db Helper
private HomeworkDbAdapter mDbHelper ;
// declarations for SQLitedatabase actions
private EditText mSubject;
private EditText mDueText;
private EditText mDescText;
private Button btnSave = null;
/**
* Called when the activity
* is first created
*/
@Override
protected void onCreate(Bundle savedInstanceState){
// called to display the reminder edit class
super.onCreate(savedInstanceState);
// open the database
mDbHelper = new HomeworkDbAdapter(this);
// set the content view by the xml layout
setContentView(R.layout.homeworkedit);
// initialize the save button
btnSave = (Button) findViewById(R.id.btnSave);
// initiate the edit text fields
mSubject = (EditText) findViewById(R.id.txtSubject);
mDueText = (EditText) findViewById(R.id.txtDueDate);
mDescText = (EditText) findViewById(R.id.txtDesc);
mRowId = savedInstanceState != null
? savedInstanceState.getLong(HomeworkDbAdapter.KEY_ROWID)
: null;
// register listeners and set the text of the buttons to their default
registerButtonListenersAndSetDefaultText();
}
private void setRowIdFromIntent(){
if(mRowId == null){
Bundle extras = getIntent().getExtras();
mRowId = extras != null
? extras.getLong(HomeworkDbAdapter.KEY_ROWID)
: null;
}
}
@Override
protected void onPause(){
mDbHelper.close();
super.onPause();
}
@Override
protected void onResume(){
super.onResume();
mDbHelper.open();
setRowIdFromIntent();
populateFields();
}
private void registerButtonListenersAndSetDefaultText() {
btnSave.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View v){
mSubject.getText();
mDescText.getText();
mDueText.getText();
saveState();
setResult(RESULT_OK);
Toast.makeText(HomeworkEditActivity.this,
getString(R.string.task_saved_message),
Toast.LENGTH_SHORT).show();
finish(); // return to list activity
}
});
}
private void populateFields() {
if(mRowId != null){
Cursor reminder = mDbHelper.fetchHomework(mRowId);
startManagingCursor(reminder);
mSubject.setText(reminder.getString(
reminder.getColumnIndexOrThrow(HomeworkDbAdapter.KEY_SUBJECT)));
mDueText.setText(reminder.getString(
reminder.getColumnIndexOrThrow(HomeworkDbAdapter.KEY_DUE)));
mDescText.setText(reminder.getString(
reminder.getColumnIndexOrThrow(HomeworkDbAdapter.KEY_DESC)));
}
if(mRowId == null){
onCreate(null);
}
}
@Override
protected void onSaveInstanceState(Bundle outState){
try{
outState.putLong(HomeworkDbAdapter.KEY_ROWID, mRowId);
}catch(NullPointerException npe){
npe.printStackTrace();
}
super.onSaveInstanceState(outState);
}
private void saveState() {
// add the save of the text reference here
String subject = mSubject.getText().toString();
String due_date = mDueText.getText().toString();
String desc = mDescText.getText().toString();
if(mRowId == null){
long id = mDbHelper.createHomework(subject, due_date, desc);
if(id > 0){
mRowId = id;
}
}else{
mDbHelper.updateHomework(mRowId, subject, due_date, desc);
}
}
}
Upvotes: 1
Views: 2124
Reputation: 37
Try switching mDbHelper.close() and super.onPause()
Like so:
@Override
protected void onPause(){
super.onPause();
mDbHelper.close();
}
99% of the time you should call the super.onPause() method first. Calling the super.onPause() does a lot of things behind the scenes. I have had rare occasions where I needed to actually remove the super method call altogether. It doesn't look like this is one of them though.
See here for more details:
http://developer.android.com/training/basics/activity-lifecycle/pausing.html#Pause
Upvotes: 0
Reputation: 8828
The fact that the NullPointerException
message contains "Unable to pause activity" means the issue is with your onPause(..)
override. I assume this happens when mDbHelper is null, although looking at your code, I can't see why it would be.
Upvotes: 1