尝试检索联系人时出现错误

问题描述

| 在我的应用程序中,我试图检索电话簿的联系人。尽管我将联系人存储在模拟器中,但查询似乎未返回任何结果。我用下面的代码:
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,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
我不知道该如何处理。谁能帮忙。谢谢     

解决方法

        使用以下代码从电话获取联系人
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);
            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,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();

        }
    }
请参阅以下URL。 将所有电话簿联系详细信息放入android中的数组 谢谢 迪帕克     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...