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

时间:2017-08-29 15:02:24

标签: android android-cursor

我的应用在我的设备上运行时崩溃了。根据logCat,Cursor无法通过以下方法从CursorWindow读取数据,该数据有2行2列:

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.access$600(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)

1 个答案:

答案 0 :(得分:0)

检查NOTES_COLUMN_ID的列名称,因为result.getColumnIndex(NOTES_COLUMN_ID)返回-1,表示在光标中找不到列名。您在CREATE TABLE声明中使用相同的常量吗?