首页 > 解决方案 > CommonDataKinds.Phone.CONTACT_ID 在联系人数据库中为空

问题描述

我认为从 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)为什么我${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} IS NOT NULL在我的 sql 查询中指定了这个错误?

2) 在什么情况下Phone.CONTACT_ID 为空?

标签: androidandroid-sqliteandroid-contentproviderandroid-contacts

解决方案


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

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

也不CONTACT_ID应该为空,所以我会删除

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

部分。


推荐阅读