2015-02-09 108 views
3

嘗試從SQLiteOpenHelper檢索所有SQLite行時,在logcat中出現以下錯誤。嘗試從SQLiteOpenHelper檢索數據時發生Android NullpointerException

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; 
} 

onCreateView這裏第89行是

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

提前致謝。

+0

SQLiteDatabase DB = this.getWritableDatabase();更改爲this.getReadableDatabase();.不應該是一個問題。試一下。並檢查databaseHandlerNote是否爲空。我認爲databaseHandlerNote是空的。 – EagleEye 2015-02-09 15:04:41

回答

5

問題在於onActivityCreated()onCreateView()之後被稱爲。因此您的databaseHandlerNote尚未創建,嘗試使用它將導致NullPointerException

查看Fragment文檔中的Fragment lifecycle diagram

+0

'onCreate()'在'onActivityCreated()之前調用' – andDev 2015-02-09 15:13:09

+0

@andDev你不在片段中使用'onCreate'。 – Tom 2015-02-09 15:17:10

+0

我沒有在這裏添加這件作品 – andDev 2015-02-09 15:20:24

2

從片段lifecircle,onCreateView()onActivityCreated()之前調用,所以當你撥打:

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

onCreateView(),尚未創建databaseHandlerNote,那麼你有例外。因此,解決方案是:

移動你:

databaseHandlerNote = new DatabaseHandlerNotes(getActivity()); 

onActivityCreated()onCreate()

相關問題