0

我正在使用联系人选择器库来选择多个联系人,但如果一个联系人不包含任何号码并且如果它被选中,那么它会在编辑文本字段中显示一些空指针异常。如何删除该消息以及如何删除尾随逗号。下面是我的代码。

 try {
            int pos = 0;
            for (Contact contact : contacts) {
                String displayName = contact.getDisplayName();
                result.append(displayName + ",");
                result.setSpan(new BulletSpan(15), pos, pos + displayName.length() + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                //pos += displayName.length() + 1;
            }
        }
        catch (Exception e) {
            result.append(e.getMessage());
        }

        contactsView.setText(result);
4

1 回答 1

0

请尝试检查此代码

 void getAllContacts() {
    ArrayList<String> nameList = new ArrayList<>();
    ArrayList<String> numberList = new ArrayList<>();

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
    String[] list = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.Contacts._ID};
    Cursor cursor = getContentResolver().query(uri, list, selection, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");

    cursor.moveToFirst();
    if (cursor.moveToFirst()) {
        do {
            String contactNumber =     cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));


            contactNuber.add(contactNumber);
            contactsName.add(contactName);
            nameList.add(contactName);
            numberList.add(contactNumber);

        } while (cursor.moveToNext());
        cursor.close();
        myContacts.put("name", nameList);
        myContacts.put("number", numberList);


    }

}
于 2016-06-22T14:42:56.007 回答