Contacts数据库中的CommonDataKinds.Phone.CONTACT_ID为空

时间:2019-01-16 15:03:28

标签: android android-sqlite android-contentprovider android-contacts

我坚信从Phone.CONTENT_URI检索联系人时Phone.CONTACT_ID列不可能为空。但是,我从用户那里收到了很多崩溃报告,例如:

Caused by java.lang.IllegalStateException: activity.contentResolver…taKinds.Phone.CONTACT_ID) must not be null

可能是因为我正在按CONTACT_ID对sql结果进行排序,所以这是示例代码

activity.contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                arrayOf(ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                        ContactsContract.CommonDataKinds.Phone.NUMBER,
                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
                        ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI,
                        ContactsContract.CommonDataKinds.Phone.IS_PRIMARY,
                        ContactsContract.CommonDataKinds.Phone.LAST_TIME_CONTACTED,
                        ContactsContract.CommonDataKinds.Phone.TIMES_CONTACTED,
                        ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
                        ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.LABEL),
                "${ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER} = ?" +
                        " AND ${ContactsContract.CommonDataKinds.Phone.NUMBER} IS NOT NULL" +
                        " AND ${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} IS NOT NULL",
                arrayOf("1"),
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID)

从我提供的代码片段的第一行抛出了异常。现在我有两个问题:

1)为什么我在SQL查询中指定了${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} IS NOT NULL时为什么会看到此错误?

2)在哪种情况下Phone.CONTACT_ID为空?

1 个答案:

答案 0 :(得分:0)

尝试将您的排序参数更改为

val sortOrder = "${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} ASC"

CONTACT_ID也永远不能为null,因此我将删除

AND ${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} IS NOT NULL

部分。