嘿我正在学习android,并且在sqLite数据库中遇到一些麻烦。我想从列到光标获取一组特定的行,并将其存储在字符串数组中以在textview中显示它。这是我用来获取光标数据的方法
public String[] getDetailedSymps(long sympId) {
String[] columns2 = new String[] { KEY_SYMP_DETAIL, KEY_SYM_ID };
String details[]= new String[9];
int i=0;
Cursor c3 = ourDatabase.query(DATABASE_TABLE2, columns2, KEY_SYM_ID + "=" + sympId, null, null, null, null);
int iDetail = c3.getColumnIndex(KEY_SYMP_DETAIL);
while (c3.isAfterLast() == false) {
details[i]= c3.getString(iDetail);
i++;
c3.moveToNext();
}
return details;
但是,当我运行代码时,它给了我错误CursorIndexOutOfBoundException,:索引-1请求大小为9,这里是logcat
08-03 03:22:06.294: W/dalvikvm(1194): threadid=1: thread exiting with uncaught exception (group=0xb4ae2ba8)
08-03 03:22:06.464: E/AndroidRuntime(1194): FATAL EXCEPTION: main
08-03 03:22:06.464: E/AndroidRuntime(1194): Process: com.example.mydb, PID: 1194
08-03 03:22:06.464: E/AndroidRuntime(1194): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mydb/com.example.mydb.DetailActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 9
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.os.Handler.dispatchMessage(Handler.java:102)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.os.Looper.loop(Looper.java:136)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-03 03:22:06.464: E/AndroidRuntime(1194): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 03:22:06.464: E/AndroidRuntime(1194): at java.lang.reflect.Method.invoke(Method.java:515)
08-03 03:22:06.464: E/AndroidRuntime(1194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-03 03:22:06.464: E/AndroidRuntime(1194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-03 03:22:06.464: E/AndroidRuntime(1194): at dalvik.system.NativeStart.main(Native Method)
08-03 03:22:06.464: E/AndroidRuntime(1194): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 9
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
08-03 03:22:06.464: E/AndroidRuntime(1194): at com.example.mydb.Dbadapter.getDetailedSymps(Dbadapter.java:296)
08-03 03:22:06.464: E/AndroidRuntime(1194): at com.example.mydb.DetailActivity.onCreate(DetailActivity.java:25)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.Activity.performCreate(Activity.java:5231)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
任何人都可以帮忙,我在这里做错了什么,在此先感谢。
答案 0 :(得分:0)
在读取数据之前,必须使用方法c3.moveToFirst()。
(顺便说一下,如果不存在行,则返回false,这是检查空游标的好方法。)