Note that there are some explanatory texts on larger screens.

plurals
  1. POIntent.ACTION_PICK returns empty cursor for some contacts
    primarykey
    data
    text
    <p>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:</p> <p>for the contacts that I entered into my contact book by hand, the <code>Intent.ACTION_PICK</code> has no trouble finding and returning them to the app, i.e. <code>cursor.moveToFirst()</code> is true.</p> <p>But for the contact that were imported by Facebook (my phone is set to sync with Facebook contacts), I get the following <code>android.database.CursorIndexOutOfBoundsException</code> 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 <code>cursor.moveToFirst()</code> false?</p> <pre><code>...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></pre> <p>Here is my code:</p> <p>dispatchIntent:</p> <pre><code>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); </code></pre> <p>onActivity result:</p> <pre><code>(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 ... } </code></pre> <p>Note: I see the contacts. But after I select the Facebook imported contact, I get the crash.</p> <p>BTW: My code snippet is copied from the android tutorial exactly: <a href="http://developer.android.com/training/basics/intents/result.html" rel="noreferrer">http://developer.android.com/training/basics/intents/result.html</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload