问题
I am trying to fetch two values from a table in sqlite db in android. I get the following exception:
05-22 04:13:05.432: I/ActivityManager(251): app/com.nykkos.personalize[INITIAL] app.idle/false
05-22 04:13:05.432: I/ActivityManager(251): START {cmp=com.nykkos.personalize/.screens.PackageInstaller} from pid 1613
05-22 04:13:05.463: W/audio_hw_primary(134): select_output_device e=0, h=0, s=2, b=0
05-22 04:13:05.502: I/ActivityManager(251): app/com.nykkos.personalize[RESUMED] app.idle/false ...realStartActivityLocked
05-22 04:13:05.526: E/SQLiteCursor(1613): requesting column name with table name -- /mnt/sdcard/googleCheckout/Saxo Trader.apk
05-22 04:13:05.526: E/SQLiteCursor(1613): java.lang.Exception
05-22 04:13:05.526: E/SQLiteCursor(1613): at android.database.sqlite.SQLiteCursor.getColumnIndex(SQLiteCursor.java:192)
05-22 04:13:05.526: E/SQLiteCursor(1613): at com.nykkos.personalize.db.SQLiteDBHelper.GetSelectedApplicationUrlList(SQLiteDBHelper.java:263)
05-22 04:13:05.526: E/SQLiteCursor(1613): at com.nykkos.personalize.screens.PackageInstaller.onCreate(PackageInstaller.java:42)
05-22 04:13:05.526: E/SQLiteCursor(1613): at android.app.Activity.performCreate(Activity.java:4465)
05-22 04:13:05.526: E/SQLiteCursor(1613): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-22 04:13:05.526: E/SQLiteCursor(1613): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
05-22 04:13:05.526: E/SQLiteCursor(1613): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1989)
05-22 04:13:05.526: E/SQLiteCursor(1613): at android.app.ActivityThread.access$600(ActivityThread.java:125)
05-22 04:13:05.526: E/SQLiteCursor(1613): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1149)
05-22 04:13:05.526: E/SQLiteCursor(1613): at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 04:13:05.526: E/SQLiteCursor(1613): at android.os.Looper.loop(Looper.java:137)
05-22 04:13:05.526: E/SQLiteCursor(1613): at android.app.ActivityThread.main(ActivityThread.java:4368)
05-22 04:13:05.526: E/SQLiteCursor(1613): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 04:13:05.526: E/SQLiteCursor(1613): at java.lang.reflect.Method.invoke(Method.java:511)
05-22 04:13:05.526: E/SQLiteCursor(1613): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-22 04:13:05.526: E/SQLiteCursor(1613): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-22 04:13:05.526: E/SQLiteCursor(1613): at dalvik.system.NativeStart.main(Native Method)
05-22 04:13:05.526: E/CursorWindow(1613): Failed to read row 0, column -1 from a CursorWindow which has 3 rows, 2 columns.
05-22 04:13:05.526: D/AndroidRuntime(1613): Shutting down VM
05-22 04:13:05.526: W/dalvikvm(1613): threadid=1: thread exiting with uncaught exception (group=0x40b0a1f8)
05-22 04:13:05.526: E/AndroidRuntime(1613): FATAL EXCEPTION: main
05-22 04:13:05.526: E/AndroidRuntime(1613): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nykkos.personalize/com.nykkos.personalize.screens.PackageInstaller}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1964)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1989)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.app.ActivityThread.access$600(ActivityThread.java:125)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1149)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.os.Looper.loop(Looper.java:137)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.app.ActivityThread.main(ActivityThread.java:4368)
05-22 04:13:05.526: E/AndroidRuntime(1613): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 04:13:05.526: E/AndroidRuntime(1613): at java.lang.reflect.Method.invoke(Method.java:511)
05-22 04:13:05.526: E/AndroidRuntime(1613): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-22 04:13:05.526: E/AndroidRuntime(1613): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-22 04:13:05.526: E/AndroidRuntime(1613): at dalvik.system.NativeStart.main(Native Method)
05-22 04:13:05.526: E/AndroidRuntime(1613): 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.
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.database.CursorWindow.nativeGetString(Native Method)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.database.CursorWindow.getString(CursorWindow.java:451)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-22 04:13:05.526: E/AndroidRuntime(1613): at com.nykkos.personalize.db.SQLiteDBHelper.GetSelectedApplicationUrlList(SQLiteDBHelper.java:263)
05-22 04:13:05.526: E/AndroidRuntime(1613): at com.nykkos.personalize.screens.PackageInstaller.onCreate(PackageInstaller.java:42)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.app.Activity.performCreate(Activity.java:4465)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-22 04:13:05.526: E/AndroidRuntime(1613): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
05-22 04:13:05.526: E/AndroidRuntime(1613): ... 11 more
05-22 04:13:05.526: W/ActivityManager(251): Force finishing activity com.nykkos.personalize/.screens.PackageInstaller
05-22 04:13:05.541: I/WindowManager(251): createSurface Window{42411fd0 paused=false}: DRAW NOW PENDING
05-22 04:13:06.025: W/ActivityManager(251): Activity pause timeout for ActivityRecord{41c0ee08 com.nykkos.personalize/.screens.PackageInstaller}
05-22 04:13:06.033: I/ActivityManager(251): app/com.nykkos.personalize[RESUMED] app.idle/true ...resumeTopActivityLocked
Following is the code I use:
public Map<String, String> GetSelectedApplicationUrlList() {
Cursor c = myDataBase.rawQuery("SELECT reco_index_user_action_download_file ," +
" reco_index_content_code from tbl_reco_index_contents where reco_index_content_type =" +
" 'Application/Widget' and reco_index_user_action_downloaded=1 and reco_index_user_action_installed =1 ",
null);
Map<String, String> sel = Collections
.synchronizedMap(new LinkedHashMap<String, String>());
if (c != null) {
if (c.moveToFirst()) {
do {
System.out.println("Url is"+c.getString(0));
System.out.println("Code is"+c.getString(1));
String url = c.getString(c.getColumnIndex(c.getString(0)));
String code = c.getString(c.getColumnIndex(c.getString(1)));
sel.put(code, url);
} while (c.moveToNext());
}
}
c.close();
return sel;
}
The exception is thrown from :
System.out.println("Url is"+c.getString(0));
When I run the same query from sqliteman, it always gives the correct result.
Any help in resolving this issue is much appreciated
Table structure is as follows:
CREATE TABLE tbl_reco_index_contents (
id integer PRIMARY KEY,
reco_index_content_code char(10),
reco_index_content_type char(30),
reco_index_user_action_downloaded smallint,
reco_index_user_action_download_file char(50),
reco_index_user_action_installed smallint
)
回答1:
The third line is the problem. See your logcat output and the Android sources (search within the sources for "requesting column name with table name"). Now have a look at your logcat output. The line with this sentence contains the column name you are requesting: /mnt/sdcard/googleCheckout/Saxo Trader.apk. This value is coming from your result cursor. Of course your db doesn't contain this colunm :-)
Substitute the lines
String url = c.getString(c.getColumnIndex(c.getString(0)));
String code = c.getString(c.getColumnIndex(c.getString(1)));
with
String url = c.getString(c.getColumnIndex("reco_index_user_action_download_file"));
String code = c.getString(c.getColumnIndex("reco_index_content_code"));
I also suggest to pull the code for getting the indices from within the loop to outside of the loop. No need to ask for the indices over and over again.
来源:https://stackoverflow.com/questions/16683453/java-lang-illegalstateexception-couldnt-read-row-0-col-1-from-cursorwindow