0

我尝试获取联系电话和姓名。我的名字是正确的,但是当我尝试获取数字时,我得到了错误。

这是代码:

public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    ArrayList<String> contactList = new ArrayList<String>();

    switch (reqCode) {
    case (0):
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor c = getContentResolver().query(contactData, null, null,
                    null, null);
            if (c.moveToFirst()) {
                String name = c
                        .getString(c
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


                String phoneNumber = c.getString(c
                        .getColumnIndex(Phone.NUMBER));

                String contact = name + "" + phoneNumber;

                contactList.add(contact);
            }
        }
        break;
    }

这是 logCat :

08-12 16:25:19.790: E/WindowManager(14381): Activity tr.com.turkcell.shmobile.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@421c0090 that was originally added here

E/WindowManager(14381): at java.lang.reflect.Method.invokeNative(Native Method) 08-12 16:25:19.790: E/WindowManager(14381): at java.lang.reflect.Method.invoke(Method. java:511) 08-12 16:25:19.790: E/WindowManager(14381): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 08-12 16:25:19.790: E/WindowManager(14381): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 08-12 16:25:19.790: E/WindowManager(14381): 在 dalvik.system.NativeStart.main (本机方法)08-12 16:25:21.370:E/CursorWindow(14381):无法从具有 1 行 29 列的 CursorWindow 读取第 0 行第 -1 列。08-12 16:25:21.370: D/AndroidRuntime(14381): 关闭 VM 08-12 16:25:21.370: W/dalvikvm(14381): threadid=1: 线程退出未捕获异常 (group=0x41b6a318) 08 -12 16:25:21.380: E/AndroidRuntime(14381): FATAL EXCEPTION: main 08-12 16:25:21.380: E/AndroidRuntime(14381): java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=0, result=-1 , data=Intent { dat=content://com.android.contacts/contacts/lookup/209i1510398b8e8d8617/1 flg=0x1 }} 到活动 {tr.com.turkcell.shmobile/tr.com.turkcell.shmobile.IndexActivity} : java.lang.IllegalStateException: 无法从 CursorWindow 读取第 0 行 col -1。确保在从光标访问数据之前正确初始化光标。08-12 16:25:21.380: E/AndroidRuntime(14381): 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3150) 08-12 16:25:21.380: E/AndroidRuntime(14381): 在 android. app.ActivityThread.handleSendResult(ActivityThread.java:3193) 08-12 16:25:21.380: E/AndroidRuntime(14381): 在 android.app.ActivityThread。internal.os.ZygoteInit.main(ZygoteInit.java:600) 08-12 16:25:21.380: E/AndroidRuntime(14381): at dalvik.system.NativeStart.main(Native Method) 08-12 16:25:21.380 :E / AndroidRuntime(14381):引起:java.lang.IllegalStateException:无法从CursorWindow读取第0行,列-1。确保在从光标访问数据之前正确初始化光标。08-12 16:25:21.380: E/AndroidRuntime(14381): 在 android.database.CursorWindow.nativeGetString(Native Method) 08-12 16:25:21.380: E/AndroidRuntime(14381): 在 android.database.CursorWindow .getString(CursorWindow.java:434) 08-12 16:25:21.380: E/AndroidRuntime(14381): 在 android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 08-12 16:25:21.380: E /AndroidRuntime(14381): 在 android.database.CursorWrapper.getString(CursorWrapper.java:114) 08-12 16:25:21.380:

4

1 回答 1

0

您需要 2 个光标来获取联系号码,为什么?.. 假设一个联系人有多个号码,你会怎么做:)?....所以你需要一个光标来浏览联系人和一个光标来浏览联系人号码的......就像@Piyush说的那样

ContentResolver cr = getContentResolver();

        Cursor cursor = cr.query(uri, projection, selection, null, sortOrder);
    while(cursor.moveToNext()){//this loops through contacts and put them in object ContactTest which i already made
        ContactTest c1 = new ContactTest();
        c1.setContactName(cursor.getString(/*name index*/));
        c1.setContactID(cursor.getString(/*id index you will need it*/));
        Cursor pCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {c1.getContactID()}, null);

        while(pCursor.moveToNext()){ // this loops through the contact number's

            c1.setContactPhone(pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        }
    }
于 2013-08-12T13:44:17.183 回答