Intent.ACTION_PICK返回空光标一些接触 [英] Intent.ACTION_PICK returns empty cursor for some contacts

查看:299
本文介绍了Intent.ACTION_PICK返回空光标一些接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,其中一个方面是用于用户选择一个联系人,并发送一个文本到该接触通该应用。该程序只适用于一些接触和失败的人。更多precisely:

I have an app in which one aspect is for a user to select a contact and send a text to that contact thru the app. The app only works with some contacts and fails on others. More precisely:

有关,我进入了我的联系簿由专人在 Intent.ACTION_PICK 联系人没有麻烦发现,并将其返回给应用程序,如 cursor.moveToFirst()是真实的。

for the contacts that I entered into my contact book by hand, the Intent.ACTION_PICK has no trouble finding and returning them to the app, i.e. cursor.moveToFirst() is true.

但对于被Facebook的进口(我的手机设置与Facebook联系人同步)的接触,我得到以下 android.database.CursorIndexOutOfBoundsException 之后,我点击接触。一个明显的问题,我已经是:为什么结果大小为0后,我从字面上选择的名片?为什么 cursor.moveToFirst()假?

But for the contact that were imported by Facebook (my phone is set to sync with Facebook contacts), I get the following android.database.CursorIndexOutOfBoundsException after I click on the contact. One glaring question I have is: why is the result size 0 after I literally selected a contact? Why is cursor.moveToFirst() false?

...Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.database.CursorWrapper.getString(CursorWrapper.java:114)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at com.company.Game.SendTextActivity.onActivityResult(SendTextActivity.java:118)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.app.Activity.dispatchActivityResult(Activity.java:5436)
05-15 17:57:04.741: E/AndroidRuntime(21301):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3188)
05-15 17:57:04.741: E/AndroidRuntime(21301):    ... 11 more

下面是我的code:

dispatchIntent:

dispatchIntent:

Intent pickContactIntent = new Intent(Intent.ACTION_PICK,  Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);

onActivity结果:

onActivity result:

(requestCode == PICK_CONTACT_REQUEST) {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = { Phone.NUMBER };

            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver()
                .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();

            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(column);

            //do work with number here ...
        }

注:我看到的联系人。但之后我选择的Facebook导入的联系人,我得到的崩溃。

Note: I see the contacts. But after I select the Facebook imported contact, I get the crash.

BTW:我的code段是从Android教程精确复制:<一href="http://developer.android.com/training/basics/intents/result.html">http://developer.android.com/training/basics/intents/result.html

BTW: My code snippet is copied from the android tutorial exactly: http://developer.android.com/training/basics/intents/result.html

推荐答案

要解决的崩溃,你应该检查moveToFirst()这样的结果:

To fix the crash you should check the result of moveToFirst() like this:

String number = null;
if (cursor.moveToFirst()) {
   number = cursor.getString(0); // 0 matches the index of NUMBER in your projection.
}

要探索,提供给你,我会通过的空的投影,使所有的领域回来的数据的性质,并转储字段名称和值。您可能会发现在号码字段您正在寻找的数据,只是没有。

To explore the nature of the data that is available to you I would pass in "null" for the projection so that all of the fields come back, and dump the field names and values. You may find the data you are looking for, just not in the NUMBER field.

这篇关于Intent.ACTION_PICK返回空光标一些接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆