1

我正在根据工作正常的帐户名称获取一个人的联系方式,但对于那些有姓名但没有号码的联系人,我得到了例外。我也正在获取这些联系人的 ID,但是当我使用 Phone cursor 中的 contact_id 获取详细信息时,它会导致异常

  private void displayAllContactsByType(String accountName) //account-name: Mobikwik
 {

    Cursor rawCursor = null;

    rawCursor = cResolver.query(
            ContactsContract.RawContacts.CONTENT_URI,
            new String[]{ContactsContract.RawContacts.CONTACT_ID},
            ContactsContract.RawContacts.ACCOUNT_NAME + "= ? AND "+ContactsContract.RawContacts.CONTACT_ID + " != 0",
            new String[]{accountName},
            ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " COLLATE LOCALIZED ASC");


    int contactIdColumn = rawCursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
    int rawCursorCount = rawCursor.getCount();
    int total = 1;

    Utils.Log("Raw Size", " " + rawCursorCount);

    while (rawCursor.moveToNext()) {

        contactId = rawCursor.getLong(contactIdColumn);
            storeContactDetails(contactId );
    }
    rawCursor.close();
}

从上述方法中,我得到了用于获取以下方法中的数据的联系人 ID。

private void storeContactDetails( Long contactId) //e.g contact_id-2512 
{

    Cursor phones = null;
    String[] projection = new String[]{
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.Contacts.LOOKUP_KEY,
            Phone.HAS_PHONE_NUMBER,
            Phone.TYPE,
            Phone.LAST_TIME_USED
    };


    phones = cResolver.query(Phone.CONTENT_URI,
            projection,
            Phone.CONTACT_ID + " = ? ",
            new String[]{String.valueOf(contactId)},
            null);

    phones.moveToFirst();
    getResultsFromPhoneCursor(phones);

}

public void getResultsFromPhoneCursor(Cursor phones) {

    String email_Id = "";
    String contactType = "";
    String lastTimeUsed = "";

    HashMap<String,String> contactTypeWithNumber=new HashMap<>();

    try {
        String hasPhone = "";
        display_name = "";
        phoneNumber = "";

        display_name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));// THIS LINE IS CAUSING EXCEPTION HERE
        lastTimeUsed = convertLongToDateFormat(phones.getString(phones.getColumnIndex(Phone.LAST_TIME_USED)));

        hasPhone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER));

        if (hasPhone.equalsIgnoreCase("1"))
            hasPhone = "true";

        else
            hasPhone = "false";

        if (Boolean.parseBoolean(hasPhone)) {

            String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

            Cursor emails = cResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Email.DATA},
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);

            while (emails.moveToNext()) {
                email_Id = emails.getString(emails
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            }

            emails.close();

            do {

                phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));

                switch (type) {

                    case 0:

                        contactType = "whatsapp";
                        break;

                    case Phone.TYPE_HOME:
                        contactType = "home";
                        break;

                    case Phone.TYPE_MOBILE:
                        contactType = "mobile";
                        break;

                    case Phone.TYPE_WORK:
                        contactType = "work";
                        break;

                    case Phone.TYPE_OTHER:
                        contactType = "other";
                        break;

                    case Phone.TYPE_WORK_PAGER:
                        contactType = "pager";
                        break;

                    case Phone.TYPE_MMS:
                        contactType = "mms";
                        break;

                    case Phone.TYPE_MAIN:
                        contactType = "main";
                        break;

                    case Phone.TYPE_FAX_HOME:
                        contactType = "fax";
                        break;

                    case Phone.TYPE_FAX_WORK:
                        contactType = "fax-home";
                        break;
                }

                if(contactList.contains(new ContactsWrapper(contactId, display_name, lookupKey, false, email_Id, lastTimeUsed,null))) {

                    try {
                        HashMap<String,String>contactTypeMapPrevious=contactList.get(contactList.size()-1).getContactTypeWithNumber();

                            if (!contactTypeMapPrevious.get(contactType).replace(" ","").equals(this.phoneNumber.replace(" ",""))) {
                                contactTypeWithNumber.put(contactType, phoneNumber);

                            }

                            else
                            {
                                if(!contactTypeWithNumber.containsKey(contactType)&& contactTypeMapPrevious.size()<=1)
                                    contactTypeWithNumber.put(contactType, phoneNumber);
                            }


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                else
                    contactTypeWithNumber.put(contactType,phoneNumber);
            }

            while (phones.moveToNext());
            contactList.add(new ContactsWrapper(contactId, display_name, lookupKey, false, email_Id, lastTimeUsed,contactTypeWithNumber));


            phones.close();
        }
    } catch (Exception e) {

        e.printStackTrace();//Cursor Out of Bound Exception here
    }

}

为什么即使我的联系人 ID 不是 0 或 null,我也会收到此异常。此代码适用于同时具有姓名和电话号码的联系人请帮助提前致谢

4

0 回答 0