第二次使用DatabaseHelper时出现NullPointerException

时间:2014-03-27 06:48:58

标签: android sqlite getwritabledatabase

我想更新数据并在片段列表中显示,数据已经传递到db,但是当使用getWritableDatabase()更新时它得到了NullPointerException,

现在我知道它不能再次调用getWritableDatabase()和getReadableDatabase(),也不需要关闭数据库。

我该怎么做才能完成它?

计算器

    public class CalcFragment extends Fragment {

    OnEqualSelectedListener mCallback;

    public interface OnEqualSelectedListener {

        public void updateResult();
    }
    ...
    private void getEqualResult() {

        textview_result.setText(mResult + sum);

        mDbHelper = new DB(getActivity());
        mDbHelper.open();
        mDbHelper.create(mResult + sum);
        mDbHelper.close();

        mCallback.updateResult();
...
}

MainActivity.class

public class MainActivity extends FragmentActivity implements CalcFragment.OnEqualSelectedListener {

    View view;
    ...
    @Override
    public void updateResult() {
        NoteFragment fragmentResult = new NoteFragment();
        fragmentResult.updateView();
        if(Debug.log){ Log.d("MainActivity", "updateResult");}
    }

DB.class

private static class DatabaseHelper extends SQLiteOpenHelper {

    ...

    public DB(Context context) {
    this.mContext = context;

    if (Debug.log) {
        Log.d("DB", "DB");
    }

}

public DB open() throws SQLException {

        dbHelper = new DatabaseHelper(mContext);
        db = dbHelper.getWritableDatabase();

    if (Debug.log) {Log.d("DB", "SQLException");
    }
    return this;
}

public void close() {

    dbHelper.close();
    if (Debug.log) {
        Log.d("DB", "close");
    }

}

public Cursor getAll() {
    mCursor = db.rawQuery("SELECT * FROM history ORDER BY created DESC",
            null);

    return mCursor;
}

列表注释

public class NoteFragment extends ListFragment {


private DB mDbHelper;
private Cursor mCursor;
private SimpleCursorAdapter adapter;

    ...

public void setAdapter(){

    mDbHelper = new DB(getActivity());
    mDbHelper.open();
    mCursor = mDbHelper.getAll();
    getActivity().startManagingCursor(mCursor);

    String[] from_column = new String[]{DB.KEY_ITEM, DB.KEY_CREATED};
    int[] to_layout = new int[]{android.R.id.text1, android.R.id.text2};

    adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,
            mCursor, from_column, to_layout);

    setListAdapter(adapter);

    if(Debug.log){ Log.d("NoteFragment", "setAdapter");}

}

public void updateView(){

    mDbHelper = new DB(getActivity());
    mDbHelper.open();
    mCursor = mDbHelper.getAll();
    getActivity().startManagingCursor(mCursor);

    String[] from_column = new String[]{DB.KEY_ITEM, DB.KEY_CREATED};
    int[] to_layout = new int[]{android.R.id.text1, android.R.id.text2};

    adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,
            mCursor, from_column, to_layout);

    adapter.changeCursor(mCursor);
    adapter.notifyDataSetChanged();

    if(Debug.log){ Log.d("NoteFragment", "updateView");}
}

日志

03-27 06:09:05.550: E/AndroidRuntime(2652): FATAL EXCEPTION: main
03-27 06:09:05.550: E/AndroidRuntime(2652): java.lang.NullPointerException
03-27 06:09:05.550: E/AndroidRuntime(2652):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
03-27 06:09:05.550: E/AndroidRuntime(2652):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
03-27 06:09:05.550: E/AndroidRuntime(2652):     at com.example.mycalc.DB.open(DB.java:85)
03-27 06:09:05.550: E/AndroidRuntime(2652):     at com.example.mycalc.NoteFragment.updateView(NoteFragment.java:85)
03-27 06:09:05.550: E/AndroidRuntime(2652):     at com.example.mycalc.MainActivity.updateResult(MainActivity.java:34)
03-27 06:09:05.550: E/AndroidRuntime(2652):     at com.example.mycalc.CalcFragment.getEqualResult(CalcFragment.java:258)
03-27 06:09:05.550: E/AndroidRuntime(2652):     at com.example.mycalc.CalcFragment.access$1(CalcFragment.java:239)
03-27 06:09:05.550: E/AndroidRuntime(2652):     at com.example.mycalc.CalcFragment$1.onClick(CalcFragment.java:171)

2 个答案:

答案 0 :(得分:0)

传递给sqlite助手的getActivity() Contextnull。这是因为您刚刚在new中使用updateResult()创建了片段,但它尚未附加到任何活动。

通常,您不应该像这样直接在片段上调用updateView()等方法。相反,等待框架调用片段生命周期回调,例如onCreateView()

答案 1 :(得分:0)

在调用fragmentResult.updateView();之前添加/替换活动中的片段。

只有在片段附加到活动后,您才能从片段内的getActivity()获得上下文。