java – 尝试从SQLiteOpenHelper检索数据时出现Android NullpointerException

尝试从SQLiteOpenHelper检索所有SQLite行时,我在logcat中遇到以下错误.

Caused by: java.lang.NullPointerException at notes.dev.tauhid.com.mynotes.fragment.MyNotes.onCreateView(MyNotes.java:89)

我的SQLiteOpenHelper类是

public class DatabaseHandlerNotes extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "my_notes";
private static final String TABLE_NOTES = "my_notes_table";
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_DESCRIPTION = "phone_number";
private static final String KEY_DATE = "date";
private static final String KEY_REMINDER_DATE = "reminder_date";
private static final String KEY_CATEGORY = "category";
private static final String KEY_LOCK = "lock";

public DatabaseHandlerNotes(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_NOTES_TABLE = "CREATE TABLE " + TABLE_NOTES + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
            + KEY_DESCRIPTION + " TEXT," + KEY_DATE + " INTEGER," + KEY_REMINDER_DATE + " INTEGER," + KEY_CATEGORY + " INTEGER," + KEY_LOCK + " TEXT" + ")";
    db.execSQL(CREATE_NOTES_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTES);

    onCreate(db);
}

public void addNote(Note note) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TITLE, note.getTitle());
    values.put(KEY_DESCRIPTION, note.getDescription());
    values.put(KEY_DATE, note.getDate());
    values.put(KEY_REMINDER_DATE, note.getReminderDate());
    values.put(KEY_CATEGORY, note.getCategory());
    values.put(KEY_LOCK, note.getLock());

    db.insert(TABLE_NOTES, null, values);
    db.close();
}

public Note getNote(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_NOTES, new String[] { KEY_ID,
                    KEY_TITLE, KEY_DESCRIPTION, KEY_DATE, KEY_REMINDER_DATE, KEY_CATEGORY }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Note note = new Note(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2), Integer.parseInt(cursor.getString(3)), Integer.parseInt(cursor.getString(4)), Integer.parseInt(cursor.getString(5)), cursor.getString(6));
    return note;
}

public List<Note> getAllNotes() {
    List<Note> noteList = new ArrayList<Note>();
    String selectQuery = "SELECT * FROM " + TABLE_NOTES;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            Note note = new Note();
            note.setID(Integer.parseInt(cursor.getString(0)));
            note.setTitle(cursor.getString(1));
            note.setDescription(cursor.getString(2));
            note.setDate(Integer.parseInt(cursor.getString(3)));
            note.setReminderDate(Integer.parseInt(cursor.getString(4)));
            note.setCategory(Integer.parseInt(cursor.getString(5)));
            note.setLock(cursor.getString(6));
            noteList.add(note);
        } while (cursor.moveToNext());
    }
    return noteList;
}

public int updateNote(Note note) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TITLE, note.getTitle());
    values.put(KEY_DESCRIPTION, note.getDescription());
    values.put(KEY_DATE, note.getDate());
    values.put(KEY_REMINDER_DATE, note.getReminderDate());
    values.put(KEY_CATEGORY, note.getCategory());
    values.put(KEY_LOCK, note.getLock());

    // updating row
    return db.update(TABLE_NOTES, values, KEY_ID + " = ?",
            new String[] { String.valueOf(note.getID()) });
}

public void deleteNote(Note note) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NOTES, KEY_ID + " = ?",
            new String[] { String.valueOf(note.getID()) });
    db.close();
}

public int getNotesCount() {
    String countQuery = "SELECT  * FROM " + TABLE_NOTES;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    return cursor.getCount();
}

在我的Fragment类中,我想要检索所有行

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    databaseHandlerNote = new DatabaseHandlerNotes(getActivity());

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.my_notes_fragment_notes, container, false);
    ListView allNotes = (ListView) rootView.findViewById(R.id.my_notes_all);
    List<Note> noteList = databaseHandlerNote.getAllNotes();
    for (Note note : noteList) {
        Note noteEach = new Note();
        noteEach.setID(note.getID());
        noteEach.setTitle(note.getTitle());
        noteEach.setDescription(note.getDescription());
        noteEach.setCategory(note.getCategory());
        noteEach.setLock(note.getLock());
        noteEach.setDate(note.getDate());
        noteEach.setReminderDate(note.getReminderDate());
        this.customNotesList.add(noteEach);
    }
    customNoteAdapter = new CustomNoteAdapter(getActivity(), customNotesList);
    allNotes.setAdapter(customNoteAdapter);
    return rootView;
}

这里第89行onCreateView是

List<Note> noteList = databaseHandlerNote.getAllNotes();

提前致谢.

解决方法:

问题是在onCreateView()之后调用onActivityCreated().因此,尚未创建databaseHandlerNote,并且尝试使用它将导致NullPointerException.

从Fragment文档中查看Fragment lifecycle diagram.

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能