无法从CursorWindow读取第1行col -1

时间:2013-06-20 07:27:03

标签: android illegalstateexception android-cursor

我正在做一个简单的Android应用程序,我需要访问手机中的联系人。

然而我的应用程序崩溃并且我收到此错误“无法读取第1行,来自CursorWindow的col -1”

这是日志:

06-19 11:03:59.582: E/CursorWindow(7926): Failed to read row 1, column -1 from a CursorWindow which has 2 rows, 29 columns.
06-19 11:03:59.611: D/AndroidRuntime(7926): Shutting down VM
06-19 11:03:59.611: W/dalvikvm(7926): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-19 11:03:59.842: E/AndroidRuntime(7926): FATAL EXCEPTION: main
06-19 11:03:59.842: E/AndroidRuntime(7926): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.Search}: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.os.Looper.loop(Looper.java:137)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at dalvik.system.NativeStart.main(Native Method)
06-19 11:03:59.842: E/AndroidRuntime(7926): Caused by: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.database.CursorWindow.nativeGetString(Native Method)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.database.CursorWindow.getString(CursorWindow.java:434)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.database.CursorWrapper.getString(CursorWrapper.java:114)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at com.example.application.Search.onCreate(Search.java:33)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.app.Activity.performCreate(Activity.java:5104)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-19 11:03:59.842: E/AndroidRuntime(7926):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-19 11:03:59.842: E/AndroidRuntime(7926):     ... 11 more

这是代码:

      Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
    people.moveToFirst();
    while(people.moveToNext()) {
       int name = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
       String contact = people.getString(name);

       int contact_no = people.getColumnIndex(PhoneLookup.NUMBER);
       String number = people.getString(contact_no);

    }

    people.close();

2 个答案:

答案 0 :(得分:0)

而不是传递null作为投影请求您需要的列

 Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
 String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER};

Cursor people = getContentResolver().query(uri, projection, null, null, null);

答案 1 :(得分:0)

正如异常所示,列号不合适。因此,根据发生异常的行,从查询返回的游标中不存在PhoneLookup.DISPLAY_NAME或PhoneLookup.NUMBER列。您可以从db显式指定所需的列,以便您知道查询是否不会抛出任何异常,那么该列肯定会存在。