我在尝试检索联系人时遇到错误

时间:2011-05-30 04:45:33

标签: android contacts

在我的应用程序中,我正在尝试检索电话簿的联系人。虽然我在模拟器上存储了联系人,但似乎查询没有返回任何结果。我使用了以下代码:

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    people.moveToFirst();
    while(people.moveToNext()) {
       int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
       String contact = people.getString(nameFieldColumnIndex);
       int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
       String number = people.getString(numberFieldColumnIndex);
       Toast.makeText(this, ""+contact+""+number, Toast.LENGTH_LONG);
    }

    people.close();

错误的日志是

E/AndroidRuntime(  277): FATAL EXCEPTION: main

E/AndroidRuntime(  277): java.lang.RuntimeException: Unable to start service mobile_security.mobile_security.ui.getSimCardContacts@44ef8960 with Intent { cmp=mobile_security.mobile_security.ui/.getSimCardContacts }: java.lang.IllegalStateException: get field slot from row 1 col -1 failed

E/AndroidRuntime(  277):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)

E/AndroidRuntime(  277):    at android.app.ActivityThread.access$3600(ActivityThread.java:125)

E/AndroidRuntime(  277):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)

E/AndroidRuntime(  277):    at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime(  277):    at android.os.Looper.loop(Looper.java:123)

E/AndroidRuntime(  277):    at android.app.ActivityThread.main(ActivityThread.java:4627)

E/AndroidRuntime(  277):    at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(  277):    at java.lang.reflect.Method.invoke(Method.java:521)

E/AndroidRuntime(  277):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

E/AndroidRuntime(  277):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

E/AndroidRuntime(  277):    at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(  277): Caused by: java.lang.IllegalStateException: get field slot from row 1 col -1 failed

E/AndroidRuntime(  277):    at android.database.CursorWindow.getString_native(Native Method)

E/AndroidRuntime(  277):    at android.database.CursorWindow.getString(CursorWindow.java:329)

E/AndroidRuntime(  277):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)

E/AndroidRuntime(  277):    at android.database.CursorWrapper.getString(CursorWrapper.java:135)

E/AndroidRuntime(  277):    at mobile_security.mobile_security.ui.getSimCardContacts.onStart(getSimCardContacts.java:40)

E/AndroidRuntime(  277):    at android.app.Service.onStartCommand(Service.java:420)

E/AndroidRuntime(  277):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)

E/AndroidRuntime(  277):    ... 10 more

我不知道如何处理这件事。有谁可以帮忙。 THX

1 个答案:

答案 0 :(得分:0)

使用以下代码从手机

获取联系人
public static void getContactNumbers(Context context) {
        String contactNumber = null;
        int contactNumberType = Phone.TYPE_MOBILE;
        String nameOfContact = null;
        if (ApplicationConstants.phoneContacts.size() <= 0) {
            ContentResolver cr = context.getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                    null, null, null);
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String id = cur.getString(cur
                            .getColumnIndex(BaseColumns._ID));
                    nameOfContact = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    if (Integer
                            .parseInt(cur.getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        Cursor phones = cr
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?", new String[] { id },
                                        null);

                        while (phones.moveToNext()) {
                            contactNumber = phones.getString(phones
                                    .getColumnIndex(Phone.NUMBER));
                            contactNumberType = phones.getInt(phones
                                    .getColumnIndex(Phone.TYPE));
                            Log.i(TAG, "...Contact Name ...." + nameOfContact
                                    + "...contact Number..." + contactNumber);

                        }
                        phones.close();
                    }

                }
            }// end of contact name cursor
            cur.close();

        }
    }

请参阅以下网址。 getting all phone book contact details into an array in android

由于 迪帕克