联系人选择器未在 Android Studio 中提供预期结果

Posted

技术标签:

【中文标题】联系人选择器未在 Android Studio 中提供预期结果【英文标题】:Contact selector not providing expected results in Android Studio 【发布时间】:2020-10-10 09:39:00 【问题描述】:

我有一段相当简单的代码可以从选择器中检索联系信息,但它没有给我预期的结果。 我得到了联系人的姓名,但没有得到号码。由于某种原因,游标返回 -1 的索引(应用程序崩溃的原因)。我可以确保联系人已填写了号码字段。我无法确定此错误的原因。

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == PICKCONTACT) 
            if (resultCode == Activity.RESULT_OK) 
                val contactData = data?.data?:throw error("error")
                val cursor = contentResolver.query(contactData, null, null, null, null)
                cursor?.moveToFirst()?:throw error("Data not found")
                val phNoIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER )
                val nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
                Log.i("phoneNo", "$nameIndex $phNoIndex")
//                recipient.put(cursor.getString(nameIndex), cursor.getString(phNoIndex) as Int)
                phone=cursor.getString(phNoIndex)
                name=cursor.getString(nameIndex)

                cursor.close()
                Log.i("info", phone+name)

                Log.i("phone", name)
            
        
    

编辑:我正在添加意图选择器请求 -

public fun pickContact(view: View)
        val intent=Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
        startActivityForResult(intent, PICKCONTACT )
    

错误如下:

Process: com.example.smssender, PID: 29366
    java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=1, result=-1, data=Intent  dat=content://com.android.contacts/contacts/lookup/745i7546045a8e2cb2bf.3789r1632-29294B4F39.847r1845-29294B4F39/1851 flg=0x1 (has extras)  to activity com.example.smssender/com.example.smssender.MainActivity: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4610)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4652)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7050)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.database.CursorWindow.nativeGetString(Native Method)
        at android.database.CursorWindow.getString(CursorWindow.java:465)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
        at android.database.CursorWrapper.getString(CursorWrapper.java:137)
        at com.example.smssender.MainActivity.onActivityResult(MainActivity.kt:54)
        at android.app.Activity.dispatchActivityResult(Activity.java:7762)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4603)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4652) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7050) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 

【问题讨论】:

发布您启动选择器意图的代码 【参考方案1】:

如果您对联系人的电话号码感兴趣,您应该使用“电话选择器”而不是“联系人选择器”。

此选择器将要求用户选择一个联系人>,然后是该联系人的电话号码。

代码如下:

Intent intent = Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE); // Note the Phone type
startActivityForResult(intent, PICKCONTACT);

以下是从 onActivityResult 意图获取信息的方法:

Uri dataUri = data.getData();
String[] projection = new String[]CommonDataKinds.Phone.NUMBER;
Cursor cursor = getContentResolver().query(dataUri, projection, null, null, null);

【讨论】:

// 注意电话类型 - 这是我很久才发现的,谢谢!

以上是关于联系人选择器未在 Android Studio 中提供预期结果的主要内容,如果未能解决你的问题,请参考以下文章

相机文件选择器未在 WebView android 中打开

引导日期选择器未在 editorFor 中显示模型值

Android Studio 文件资源管理器未显示数据文件夹中的文件

JQuery Mobile 日期选择器未在 Chrome 中显示日期

Android Kotlin:定期工作管理器未在后台定期运行该功能

Xamarin 表单文件选择器未在 ios 上显示相机胶卷或图像库