从SQLite数据库读取时出现CursorIndexOutOfBoundsException

时间:2013-12-30 12:54:13

标签: android sqlite android-sqlite

我正试图从SQLite数据库中获取dara,每当我尝试执行此操作时,我的应用程序都会崩溃。这是logcat输出:

12-30 07:17:15.194: E/AndroidRuntime(9412): FATAL EXCEPTION: main
12-30 07:17:15.194: E/AndroidRuntime(9412): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testparsing/com.example.testparsing.Urnik}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.os.Looper.loop(Looper.java:137)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at java.lang.reflect.Method.invokeNative(Native Method)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at java.lang.reflect.Method.invoke(Method.java:525)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at dalvik.system.NativeStart.main(Native Method)
12-30 07:17:15.194: E/AndroidRuntime(9412): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at com.example.testparsing.Urnik.onCreate(Urnik.java:20)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.app.Activity.performCreate(Activity.java:5133)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-30 07:17:15.194: E/AndroidRuntime(9412):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-30 07:17:15.194: E/AndroidRuntime(9412):

从光标读取(应用程序在第二行崩溃):

Cursor c = db.getSubject("Ponedeljek", 2, "4c");
String predmet = c.getString(c.getColumnIndex(SQLiteHelper.PREDMET));

getSubject函数:

Cursor getSubject(String dan, int ura, String oddelek){
    String[] columns = new String[]{SQLiteHelper.PREDMET};
    String selection = SQLiteHelper.DAN+"=? and "+SQLiteHelper.URA+"=? and "+SQLiteHelper.ODDELEK+"=?";
    String[] selectionArgs = new String[]{dan, Integer.toString(ura), oddelek};
    open();
    return db.query(
            SQLiteHelper.IME_TABELE, 
            columns, 
            selection, 
            selectionArgs,
            null, null, null);
}

2 个答案:

答案 0 :(得分:2)

从Cursor阅读的最佳做法:

if ( !cursor.moveToFirst() ) {
//no rows to read
}

do {
    String foo = cursor.getString(..); }
while( cursor.moveToNext() );

答案 1 :(得分:0)

在访问光标列值之前,您必须使用例如移动它来将其移动到指向有效行。 moveToFirst()moveToNext()。如果光标指向调用后的有效行,这些方法将返回true

相关问题