0

我想更新数据并显示在片段列表中,数据已经传入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");}
    }

数据库类

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)
4

2 回答 2

0

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

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

于 2014-03-27T06:55:55.043 回答
0

getActivity() Context传递给 sqlite 助手的是null. 这是因为您刚刚使用 in 创建了片段,new并且updateResult()它还没有附加到任何活动。

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

于 2014-03-27T06:51:34.673 回答