0

我正在尝试从 android 的 sqlite db 中的表中获取两个值。我得到以下异常:

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

以下是我使用的代码:

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;
}

异常从以下位置抛出:

System.out.println("Url is"+c.getString(0));

当我从 sqliteman 运行相同的查询时,它总是给出正确的结果。

非常感谢解决此问题的任何帮助

表结构如下:

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
)
4

1 回答 1

1

第三行是问题。查看您的 logcat 输出和Android 源(在源中搜索“请求具有表名的列名”)。现在看看你的 logcat 输出。这句话所在的行包含您请求的列名:/mnt/sdcard/googleCheckout/Saxo Trader.apk。该值来自您的结果光标。当然,您的数据库不包含此列 :-)

替换行

     String url = c.getString(c.getColumnIndex(c.getString(0)));
     String code = c.getString(c.getColumnIndex(c.getString(1)));

     String url = c.getString(c.getColumnIndex("reco_index_user_action_download_file"));
     String code = c.getString(c.getColumnIndex("reco_index_content_code"));

我还建议将用于从循环内获取索引的代码拉到循环外。无需一遍又一遍地询问索引。

于 2013-05-23T19:30:06.607 回答