我是Android的新手,正在尝试开发我的第一个生产应用程序。这是一个用户定义的数据存储应用程序,由一个列表活动和一个添加对话框活动以及一个数据库适配器组成。我所有的“ CRUD”(创建,读取,更新,删除)功能似乎都能正常工作。但是,当数据没有保存到数据库时,更改添加对话框的方向时,遇到了以下错误。换句话说,当我将方向从纵向更改为横向时,我的onSavedInstanceState()函数失败了,反之亦然。
02-16 16:42:05.321:E / AndroidRuntime(347):java.lang.RuntimeException:无法暂停活动{net.learn2develop.Homework / net.learn2develop.Homework.HomeworkEditActivity}:java.lang.NullPointerException
这是我的代码:
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);
}
}
}
最佳答案
NullPointerException
消息包含“无法暂停活动”的事实意味着问题出在您的onPause(..)
替代项上。我认为这是在mDbHelper为null时发生的,尽管查看您的代码,但我看不出为什么会这样。