Android: Crash after choosing a contact
<p>I am developing an android application(using Eclipse 3.5.2, Android 2.2) where i need to launch built-in contact from my app and pick a contact from there in a module. I am using the following code for that. I am fine launching to built-in contact, but when i select a contact, it comes back to 'onActivityResult' in my project, results crash as "Application stopped unexpectedly, force close". I am testing on emulator with only one test contact added. Crash is happening at this line <strong>"Cursor cursor = managedQuery(intent.getData(), null, null, null, null);"</strong></p>

<p>Could someone please tell me what i'm doing wrong here?</p>

<pre><code>int PICK_CONTACT;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    SetupView();
}
private void SetupView()
{
    final Button selectContactBtn = (Button) findViewById(R.id.selectcontactBtn);
    //selectContactBtn.setText("Test Button Text");
    selectContactBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
            startActivityForResult(intent, PICK_CONTACT);
        }
    });
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
    if ( requestCode==PICK_CONTACT &amp;&amp; intent != null)
    {
        getContactInfo(intent);
    }
}
protected void getContactInfo(Intent intent)
{
    Cursor cursor = managedQuery(intent.getData(), null, null, null, null);// CRASH HERE
    cursor.moveToNext();
    String contactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
    Toast.makeText(this, "Contact Name: "+name, Toast.LENGTH_LONG).show();
}

LOGCAT:

08-13 18:15:45.075: INFO/ActivityManager(58): Displayed activity com.jonsofttech.smsacontact/.SMSaContactActivity: 6579 ms (total 6579 ms)
08-13 18:16:00.225: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.PICK typ=vnd.android.cursor.dir/contact cmp=com.android.contacts/.ContactsListActivity }
08-13 18:16:00.756: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44feba98 com.jonsofttech.smsacontact/.SMSaContactActivity}
08-13 18:16:00.925: INFO/ContactsListActivity(159): Called with action: android.intent.action.PICK
08-13 18:16:02.215: INFO/ActivityManager(58): Displayed activity com.android.contacts/.ContactsListActivity: 1461 ms (total 1461 ms)
08-13 18:16:22.695: WARN/ActivityManager(58): Launch timeout has expired, giving up wake lock!
08-13 18:16:22.775: WARN/ActivityManager(58): Activity idle timeout for HistoryRecord{44feba98 com.jonsofttech.smsacontact/.SMSaContactActivity}
08-13 18:16:35.015: ERROR/DatabaseUtils(159): Writing exception to parcel
08-13 18:16:35.015: ERROR/DatabaseUtils(159): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts/lookup/0r1-4A4E2C2E2C402C4E444A/1 from pid=956, uid=10036 requires android.permission.READ_CONTACTS
08-13 18:16:35.015: ERROR/DatabaseUtils(159):     at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:271)
08-13 18:16:35.015: ERROR/DatabaseUtils(159):     at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:149)
08-13 18:16:35.015: ERROR/DatabaseUtils(159):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111)
08-13 18:16:35.015: ERROR/DatabaseUtils(159):     at android.os.Binder.execTransact(Binder.java:288)
08-13 18:16:35.015: ERROR/DatabaseUtils(159):     at dalvik.system.NativeStart.run(Native Method)
08-13 18:16:51.146: WARN/dalvikvm(956): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): FATAL EXCEPTION: main
08-13 18:16:54.216: ERROR/AndroidRuntime(956): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-4A4E2C2E2C402C4E444A/1 (has extras) }} to activity {com.jonsofttech.smsacontact/com.jonsofttech.smsacontact.SMSaContactActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts/lookup/0r1-4A4E2C2E2C402C4E444A/1 from pid=956, uid=10036 requires android.permission.READ_CONTACTS
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.app.ActivityThread.access$2800(ActivityThread.java:125)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.os.Looper.loop(Looper.java:123)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at java.lang.reflect.Method.invoke(Method.java:521)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at dalvik.system.NativeStart.main(Native Method)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts/lookup/0r1-4A4E2C2E2C402C4E444A/1 from pid=956, uid=10036 requires android.permission.READ_CONTACTS
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.os.Parcel.readException(Parcel.java:1247)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.content.ContentResolver.query(ContentResolver.java:245)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.app.Activity.managedQuery(Activity.java:1520)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at com.jonsofttech.smsacontact.SMSaContactActivity.getContactInfo(SMSaContactActivity.java:47)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at com.jonsofttech.smsacontact.SMSaContactActivity.onActivityResult(SMSaContactActivity.java:42)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.app.Activity.dispatchActivityResult(Activity.java:3890)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
08-13 18:16:54.216: ERROR/AndroidRuntime(956):     ... 11 more
08-13 18:16:54.475: WARN/ActivityManager(58):   Force finishing activity com.jonsofttech.smsacontact/.SMSaContactActivity
08-13 18:16:55.136: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44feba98 com.jonsofttech.smsacontact/.SMSaContactActivity}
08-13 18:17:07.556: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{44feba98 com.jonsofttech.smsacontact/.SMSaContactActivity}
</code></pre>
