字段0,-1的错误请求。 numRows = 164,numColumns = 63

时间:2014-02-07 12:21:49

标签: android sqlite contacts contactscontract

您好我正在尝试通过contentprovider从Contacts Table获取数据,但每次启动应用时都会崩溃,

这是我的源代码:

public class WebpreneurActivity extends ListActivity {



        private DBHandler dbHelper;

            private Cursor phones;
            ImageButton imageButton;
             public static long  rowId;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                Log.d("database1" ,"0");
                Log.d("Your Location4", "ok4:");
                super.onCreate(savedInstanceState);
                Log.d("database1" ,"1");
                setContentView(R.layout.activity_webpreneur);
                Log.d("database1" ,"2");
                //dbHelper = new DBHandler(this);
                Log.d("database1" ,"3");
            //  dbHelper.open();
                  phones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.Contacts._ID, null,
                            ContactsContract.Contacts.DISPLAY_NAME + " ASC");
                  if (phones.getCount() > 0)  {
                        while (phones.moveToFirst()) {

                            if (Integer.parseInt(phones.getString(phones
                                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {


                                    String name = phones
                                            .getString(phones
                                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                                    String phoneNumber = phones
                                            .getString(phones
                                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                    String contactId = phones
                                            .getString(phones
                                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                                    System.out.println("contactId="+ contactId+ ", name=" + name + ", phoneNumber=" + phoneNumber);

                            }

                        }
                        phones.close();

                    }}}

我的Logcat是:

02-07 17:00:54.905: D/database1(19505): 0
02-07 17:00:54.905: D/Your Location4(19505): ok4:
02-07 17:00:54.905: D/database1(19505): 1
02-07 17:00:54.985: D/database1(19505): 2
02-07 17:00:54.985: D/database1(19505): 3
02-07 17:00:55.045: E/CursorWindow(19505): Bad request for field slot 0,-1. numRows = 164, numColumns = 63
02-07 17:00:55.045: D/AndroidRuntime(19505): Shutting down VM
02-07 17:00:55.045: W/dalvikvm(19505): threadid=1: thread exiting with uncaught exception (group=0x4001d578)
02-07 17:00:55.085: E/AndroidRuntime(19505): FATAL EXCEPTION: main
02-07 17:00:55.085: E/AndroidRuntime(19505): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webpreneur_contactlist/com.webpreneur_contactlist.WebpreneurActivity}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.os.Looper.loop(Looper.java:138)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.app.ActivityThread.main(ActivityThread.java:3701)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at java.lang.reflect.Method.invokeNative(Native Method)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at java.lang.reflect.Method.invoke(Method.java:507)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at dalvik.system.NativeStart.main(Native Method)
02-07 17:00:55.085: E/AndroidRuntime(19505): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.database.CursorWindow.getString_native(Native Method)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.database.CursorWindow.getString(CursorWindow.java:329)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.database.CursorWrapper.getString(CursorWrapper.java:135)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at com.webpreneur_contactlist.WebpreneurActivity.onCreate(WebpreneurActivity.java:58)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-07 17:00:55.085: E/AndroidRuntime(19505):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
02-07 17:00:55.085: E/AndroidRuntime(19505):    ... 11 more
02-07 17:04:53.027: I/Process(19505): Sending signal. PID: 19505 SIG: 9  

1 个答案:

答案 0 :(得分:2)

只是不要将ContactsContract.Contacts._ID传递给query()请求:

phones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            null, null,
                            ContactsContract.Contacts.DISPLAY_NAME + " ASC");

<强>更新: 实际上你的代码还有一些问题

ContactsContract.Contacts.HAS_PHONE_NUMBER表中没有ContactsContract.CommonDataKinds.Phone列。您应该只需ContactsContract.CommonDataKinds.Phone.NUMBER并检查它是否为空。

此外,您应该使用while (phones.moveToNext())代替while (phones.moveToFirst())

请尝试以下代码:

phones = getContentResolver().query(
             ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
             null, null,
             ContactsContract.Contacts.DISPLAY_NAME + " ASC");
   if (phones.getCount() > 0)  {
         while (phones.moveToNext()) {

             String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
             if (!TextUtils.isEmpty(phoneNumber)) {


                     String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                     String contactId = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                     System.out.println("contactId="+ contactId+ ", name=" + name + ", phoneNumber=" + phoneNumber);

             }

         }