加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 04:25:23 所属栏目:Java 来源:网络整理
导读:尝试从SQLiteOpenHelper检索所有SQLite行时,我在logcat中遇到以下错误. Caused by: java.lang.NullPointerException at notes.dev.tauhid.com.mynotes.fragment.MyNotes.onCreateView(MyNotes.java:89) 我的SQLiteOpenHelper类是 public class DatabaseHandl
尝试从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,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);
    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.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,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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读