javaandroidcontactscontactpicker

Pick Contacts onClick when Button is pressed Android


Im having issues with my App. What i'd like to do is when my Button is pressed to open my Phone Contatcs and display them to my TextViews. I have issues when i perform the Button Press i think.

At least LogCat is saying me some Error with my Button but i can't figure out whats wrong.

In my Manifestfile i've already set the permissions for reading and writing contacts. I have a ContactView.java where the Code happens and i've already created the view with Button and 2 textView's.

Maybe someone can help me or at least tell me what im doing wrong.

In my view i said: android:onClick="pickContact"

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class ContactView extends AppCompatActivity {
private static final int RESULT_PICK_CONTACT = 85500;
private TextView textView1;
private TextView textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_view);
    textView1 = (TextView) findViewById(R.id.TxtName);
    textView2 = (TextView) findViewById(R.id.TxtNumber);
}
public void pickContact(View v)
{
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // check whether the result is ok
    if (resultCode == RESULT_OK) {
        // Check for the request code, we might be usign multiple startActivityForReslut
        switch (requestCode) {
            case RESULT_PICK_CONTACT:
                contactPicked(data);
                break;
        }
    } else {
        Log.e("ContactView", "Failed to pick contact");
    }
}
/**
 * Query the Uri and read contact details. Handle the picked contact data.
 * @param data
 */
private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null ;
        String name = null;
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        // column index of the contact name
        int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);
        // Set the value to the textviews
        textView1.setText(name);
        textView2.setText(phoneNo);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This is what LogCat says after i press my addcontacts Button while debugging:

    10-18 23:11:08.684    4353-4353/me.resq.resqme E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: me.resq.resqme, PID: 4353
        java.lang.IllegalStateException: Could not execute method of the activity
                at android.view.View$1.onClick(View.java:3969)
                at android.view.View.performClick(View.java:4640)
                at android.view.View$PerformClick.run(View.java:19421)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5476)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.reflect.InvocationTargetException
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at android.view.View$1.onClick(View.java:3964)
                at android.view.View.performClick(View.java:4640)
                at android.view.View$PerformClick.run(View.java:19421)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5476)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
                at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:746)
                at me.resq.resqme.ContactView.pickContact(ContactView.java:28)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at android.view.View$1.onClick(View.java:3964)
                at android.view.View.performClick(View.java:4640)
                at android.view.View$PerformClick.run(View.java:19421)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5476)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                at dalvik.system.NativeStart.main(Native Method)

Solution

  • private static final int RESULT_PICK_CONTACT = 85500;
    

    Please try to change it to something lower like:

    private static final int RESULT_PICK_CONTACT = 85;