错误无法从 CursorWindow 读取第 0 行第 -1 列

Error Can't read row 0 column -1 from CursorWindow

我的应用程序在我的设备上 运行 时崩溃了。根据logCat,Cursor无法从2行2列的CursorWindow中读取数据,方法如下:

public ArrayList<Notes> getData() {
            SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<Notes> listNotes = new ArrayList<Notes>();
            result = db.rawQuery("SELECT * from " + NOTES_TABLE_NAME, new String[] {});
            if (result != null && result.getCount() > 0) {
                if (result.moveToFirst()) {
                    do {
                    Notes note = new Notes();
                    note.id = result.getInt(result.getColumnIndex(NOTES_COLUMN_ID));
                    note.text = result.getString(result.getColumnIndex(NOTES_COLUMN_NAME));
                    listNotes.add(note);
                } while (result.moveToNext());
                }
            }
            result.close();
            return listNotes;
    }

logCat

15:50:35.673    5531    agenda.com  ERROR   CursorWindow    Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 2 columns.
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime  FATAL EXCEPTION: main
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime  java.lang.RuntimeException: Unable to start activity ComponentInfo{agenda.com/agenda.com.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2446)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread.access0(ActivityThread.java:165)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.os.Handler.dispatchMessage(Handler.java:107)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.os.Looper.loop(Looper.java:194)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread.main(ActivityThread.java:5434)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at java.lang.reflect.Method.invokeNative(Native Method)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at java.lang.reflect.Method.invoke(Method.java:525)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at dalvik.system.NativeStart.main(Native Method)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime  Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.database.CursorWindow.nativeGetLong(Native Method)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.database.CursorWindow.getLong(CursorWindow.java:507)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.database.CursorWindow.getInt(CursorWindow.java:574)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at agenda.com.db.NotesDbHelper.getData(NotesDbHelper.java:52)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at agenda.com.MainActivity.onCreate(MainActivity.java:59)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.Activity.performCreate(Activity.java:5122)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)

检查 NOTES_COLUMN_ID 的列名,因为 result.getColumnIndex(NOTES_COLUMN_ID) 返回 -1,这意味着在您的游标中找不到该列名。您是否在 CREATE TABLE 语句中使用相同的常量?