无法从 CursorWindow 读取第 0 行,第 5 列。在从游标访问数据之前确保正确初始化了游标

Couldn't read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it

Log Cat

Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetString(Native Method) at android.database.CursorWindow.getString(CursorWindow.java:465) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) at com.example.workhours.DataBaseHelper.ViewAllNotes(DataBaseHelper.java:90) at com.example.workhours.MainActivity.ViewAllNotes(MainActivity.java:55) at com.example.workhours.MainActivity.onCreate(MainActivity.java:37)

public  ArrayList<newNote> ViewAllNotes() {
    ArrayList<newNote> arrayList = new ArrayList<>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT NOTEMEMOS FROM " + TABLE_NAME, null);

    while(cursor.moveToNext()){
        String notes = cursor.getString(5);
        newNote newNote = new newNote(notes);
        arrayList.add(newNote);
    }
    return arrayList;
}

查询

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, DATE TEXT, TIMESHIFTSTART INTEGER, TIMESHIFTENDS TEXT, NOTES TEXT, NOTEMEMOS TEXT)");
}

尝试在获取任何数据之前将光标移动到第一个,并且您只选择了数据库中的列,但您要求的是第 5 个。

public  ArrayList<newNote> ViewAllNotes() {
ArrayList<newNote> arrayList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT NOTEMEMOS FROM " + TABLE_NAME, null);

cursor.moveToFirst();

newNote newnote = new newNote(cursor.getString(0));
arrayList.add(newnote);

while(cursor.moveToNext()){
    String notes = cursor.getString(0);
    newNote newnote = new newNote(notes);
    arrayList.add(newnote);
}
return arrayList;
}

您的光标只有一列 SELECT NOTEMEMOS,但您正在尝试阅读第六列 getString(5)。将其替换为 getString(0) 以读取唯一的列值。