Con este código hago que se rellene un editText seleccionando un contacto de entre todos los de la agenda.
con1 = (EditText) findViewById(R.id.con1);
con1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK);
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, PICK_NUMBER);
}});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_NUMBER){
Uri contactUri = data.getData();
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phoneNumber = cursor.getString(column);
if(phoneNumber != null)
phoneNumber = phoneNumber.trim();
if(phoneNumber == null || phoneNumber.equals("")) {
// This should never happen, but just in case we'll handle it
return;
}
// Retrieve the contact name.
column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Identity.DISPLAY_NAME);
String name = cursor.getString(column);
con1.setText(phoneNumber+" - "+name);
cursor.close();
}
}
La selección funciona perfectamente, pero si se pulsa en el editText, se abre la agenda, Y NO SE SELCCIONA CONTACTO, lanza el siguiente error:
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {midlab.ap/midlab.ap.NBas}: java.lang.NullPointerException at android.app.ActivityThread.deliverResults(ActivityThread.java:3406) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449) at android.app.ActivityThread.access$1200(ActivityThread.java:150) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:175) at android.app.ActivityThread.main(ActivityThread.java:5279) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at midlab.ap.NBas.onActivityResult(NBas.java:491) at android.app.Activity.dispatchActivityResult(Activity.java:5472) at android.app.ActivityThread.deliverResults(ActivityThread.java:3402) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449) at android.app.ActivityThread.access$1200(ActivityThread.java:150) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:175) at android.app.ActivityThread.main(ActivityThread.java:5279) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method)
Tengo claro que tengo que manejar el error en:
if(phoneNumber == null || phoneNumber.equals("")) {
// This should never happen, but just in case we'll handle it
return;
}
pero no sé que poner, no se si hay que hacer cancel(), dismiss() o algo similar.
¿Alguien sabe cómo manejar este error?
Muchas gracias