我试图将所有联系人(姓名和电话号码)从我的手机发送到字符串数组。我尝试了这部分代码,但是我收到了一个错误...

Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String aNameFromContacts[] = new String[contacts.getCount()];
String aNumberFromContacts[] = new String[contacts.getCount()];

int ij = 0;

int nameFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.DISPLAY_NAME);
int numberFieldColumnIndex = contacts.getColumnIndex(PhoneLookup.NUMBER);

while(contacts.moveToNext()) {
    String contactName = contacts.getString(nameFieldColumnIndex);
    aNameFromContacts[ij] = contactName ;
    String number = contacts.getString(numberFieldColumnIndex);
    aNumberFromContacts[ij] = number ;
    ij++;
    contacts.close();


这是错误:

08-31 18:09:09.031: E/CursorWindow(16317): Failed to read row 0, column -1 from a CursorWindow which has 109 rows, 44 columns.


我该如何解决?还是有更好的方法?

最佳答案

始终使用contacts.getColumnIndexOrThrow(PhoneLookup.NUMBER);

代替contacts.getColumnIndex(PhoneLookup.NUMBER);

这样您可以获得确切的错误详细信息。在这种情况下是:

java.lang.IllegalArgumentException: column 'number' does not exist

表示您正在尝试获取不存在的列的索引,因此您的getColumnIndex方法返回的结果为null。

以下代码段将为您提供所需的内容:

ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    String aNameFromContacts[] = new String[cur.getCount()];
    String aNumberFromContacts[] = new String[cur.getCount()];
    int j = 0;
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
                    .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
            String name = cur
                    .getString(cur
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

            if (("1")
                    .equals(cur.getString(cur
                            .getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER)))) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                int i = 0;
                int pCount = pCur.getCount();
                String[] phoneNum = new String[pCount];
                String[] phoneType = new String[pCount];
                while (pCur.moveToNext()) {
                    phoneNum[i] = pCur
                            .getString(pCur
                                    .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    phoneType[i] = pCur
                            .getString(pCur
                                    .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE));

                    i++;
                }
                if (phoneNum.length > 0) {
                    aNumberFromContacts[i] = phoneNum[0];
                    aNameFromContacts[j] = name;
                    Log.d("name = ", "" + name +", phoneNumber = " + phoneNum[0]);
                }
            }
            j++;
        }
    }


另外,请尝试理解此代码,以便您了解其工作方式以及什么是phoneNum和phoneType。

PS:可以根据需要对该代码进行优化以提高性能。

关于java - 获取联系人到字符串数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25594030/

10-10 02:44