android - 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 为空?
解决方案
尝试将您的排序参数更改为
val sortOrder = "${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} ASC"
也不CONTACT_ID
应该为空,所以我会删除
AND ${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} IS NOT NULL
部分。
推荐阅读
- sql - 使用拆分从 MS SQL 中删除
- gradle - 未为任务启用 Gradle 构建缓存
- swift - 如何在段索引 chnage 上隐藏 ButtonBarPagerTabStripViewController 的 Childe View Controller
- ios - XCS_PRODUCT 路径中缺少 Xcode 9 bot 集成 ipa
- tensorflow - TensorFlow 对象检测 API 测试错误
- bash - Gradle 环境变量。从文件加载
- docker - RUN curl 在 dockerfile 中失败,但可以从命令行运行
- html - 如何在 wordpress 上编码这个 html
- javascript - JavaScript 标头和原型
- c - 如何做一个将文件中单词的第一个字符大写的 C 程序?