比如说,联系人里面有个叫“jack"的人,只有名字,没有电话号码。
val queryFields2 = arrayOf(ContactsContract.CommonDataKinds.Phone.NUMBER) val phoneCursor = requireActivity().contentResolver .query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, queryFields2, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null ) phoneCursor?.use { if (phoneCursor.count > 0) { phoneCursor.moveToFirst() Log.d("CrimeFragment", "测试") crime.suspectPhoneNumber = phoneCursor.getString(0) } else{ crime.suspectPhoneNumber = "" }
那么cursor的count会为0,因为找不到那个id的phone number。