当我尝试从SQLite检索到数组时出现错误。 我在for循环中找出了错误原因,声明中的错误
urlString[i] = allrows.getString(0);
我试图调试它,但我仍然不知道如何解决它。方法如下:
public String[] getImgURL(){
mydb = localContext.openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
Cursor allrows = mydb.rawQuery("SELECT ImgURL FROM "+ TABLE, null);
String[] urlString = new String[allrows.getCount()-1];
for (int i = 1; i < allrows.getCount(); i++) {
urlString[i] = allrows.getString(0);
allrows.moveToNext();
}
allrows.close();
mydb.close();
return urlString;
}
日志如下
03-06 01:26:05.479: W/dalvikvm(27671): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
03-06 01:26:05.479: E/AndroidRuntime(27671): FATAL EXCEPTION: main
03-06 01:26:05.479: E/AndroidRuntime(27671): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testing/com.ip.scan.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.app.ActivityThread.access$1500(ActivityThread.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.os.Handler.dispatchMessage(Handler.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.os.Looper.loop(Looper.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.app.ActivityThread.main(ActivityThread.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 01:26:05.479: E/AndroidRuntime(27671): at java.lang.reflect.Method.invoke(Method.java:507)
03-06 01:26:05.479: E/AndroidRuntime(27671): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at dalvik.system.NativeStart.main(Native Method)
03-06 01:26:05.479: E/AndroidRuntime(27671): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.database.AbstractCursor.checkPosition(AbstractCursor.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): at com.ip.scan.LocalSQLite.getImgURL(LocalSQLite.java:57)
03-06 01:26:05.479: E/AndroidRuntime(27671): at com.ip.scan.MainActivity.onCreate(MainActivity.java:32)
03-06 01:26:05.479: E/AndroidRuntime(27671): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
03-06 01:26:05.479: E/AndroidRuntime(27671): ... 12 more
03-06 01:26:05.549: D/dalvikvm(27671): GC_CONCURRENT freed 220K, 41% free 3334K/5639K, external 0K/0K, paused 3ms+2ms
03-06 01:26:05.549: E/Database(27671): close() was never explicitly called on database '/data/data/com.test.testing/databases/vege.db'
03-06 01:26:05.549: E/Database(27671): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-06 01:26:05.549: E/Database(27671): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java)
03-06 01:26:05.549: E/Database(27671): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java)
03-06 01:26:05.549: E/Database(27671): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java)
03-06 01:26:05.549: E/Database(27671): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java)
03-06 01:26:05.549: E/Database(27671): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java)
03-06 01:26:05.549: E/Database(27671): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java)
03-06 01:26:05.549: E/Database(27671): at com.ip.scan.LocalSQLite.getImgURL(LocalSQLite.java:53)
03-06 01:26:05.549: E/Database(27671): at com.ip.scan.MainActivity.onCreate(MainActivity.java:32)
03-06 01:26:05.549: E/Database(27671): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
03-06 01:26:05.549: E/Database(27671): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
03-06 01:26:05.549: E/Database(27671): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
03-06 01:26:05.549: E/Database(27671): at android.app.ActivityThread.access$1500(ActivityThread.java)
03-06 01:26:05.549: E/Database(27671): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
03-06 01:26:05.549: E/Database(27671): at android.os.Handler.dispatchMessage(Handler.java)
03-06 01:26:05.549: E/Database(27671): at android.os.Looper.loop(Looper.java)
03-06 01:26:05.549: E/Database(27671): at android.app.ActivityThread.main(ActivityThread.java)
03-06 01:26:05.549: E/Database(27671): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 01:26:05.549: E/Database(27671): at java.lang.reflect.Method.invoke(Method.java:507)
03-06 01:26:05.549: E/Database(27671): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
03-06 01:26:05.549: E/Database(27671): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
03-06 01:26:05.549: E/Database(27671): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
试试这个,
public String[] getImgURL(){
mydb = localContext.openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
Cursor allrows = mydb.rawQuery("SELECT ImgURL FROM "+ TABLE, null);
String[] urlString = new String[allrows.getCount()-1];
allrows.moveToFirst();
for (int i = 1; i < allrows.getCount(); i++) {
urlString[i] = allrows.getString(0);
allrows.moveToNext();
}
allrows.close();
mydb.close();
return urlString;
}
答案 1 :(得分:0)
如果您的错误是一个超出范围的索引,我的猜测是您正在创建一个大小为n -1的数组,然后将n个元素放入其中。它应该是
String[] urlString = new String[allrows.getCount()];
这样,数组足够大,可以容纳你放入的元素数量。如果这是错误的,请发布一个堆栈跟踪,在黑暗中刺伤并不是很有趣。
答案 2 :(得分:0)
在开始使用光标操作之前,您需要拨打allrows.moveToFirst
。
答案 3 :(得分:0)
这里有两个问题。
首先,您必须先移动光标。 moveToFirst或moveToNext很好。
其次,数组中的索引以及数组的大小不正确。
这样做的正确和最新情况如下:
mydb = localContext.openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
Cursor allrows = mydb.rawQuery("SELECT ImgURL FROM "+ TABLE, null);
List<String> result = new ArrayList<String>();
while (allrows.moveToNext()) {
result.add(allrows.getString(0));
}
allrows.close();
mydb.close();
return allrows.toArray(new String[0]);