Activity中的NullPointerException

时间:2012-02-16 17:08:08

标签: android nullpointerexception orientation

我对Android有点新,我正在尝试开发我的第一个生产应用程序。它是一个用户定义的数据存储应用程序,由列表活动和添加对话框活动以及数据库适配器组成。我的所有“CRUD”(创建,读取,更新,删除)功能似乎完美无缺。但是当我没有将数据保存到数据库时更改添加对话框的方向时,我遇到了以下错误。换句话说,当我将方向从纵向更改为横向或反之时,我的onSavedInstanceState()函数失败。

02-16 11: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);
    }
}

}

2 个答案:

答案 0 :(得分:1)

NullPointerException消息包含“无法暂停活动”这一事实意味着问题在于您的onPause(..)覆盖。我假设当mDbHelper为空时会发生这种情况,虽然查看你的代码,我看不出它为什么会这样。

答案 1 :(得分:0)

尝试切换mDbHelper.close()和super.onPause()

像这样:

@Override
protected void onPause(){
    super.onPause();
    mDbHelper.close();
}

99%的时间你应该首先调用super.onPause()方法。调用super.onPause()会在幕后做很多事情。在极少数情况下,我需要完全删除超级方法调用。虽然这看起来不是其中之一。

请点击此处了解更多详情:

http://developer.android.com/training/basics/activity-lifecycle/pausing.html#Pause