Reputation: 131
Error while i choose a contact from the contact list . Any help from you will be nice ... Here is the code for receiving number from contact and showing it in edit text view :
private void importContact() {
Intent importContactIntent = new Intent(Intent.ACTION_PICK);
importContactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(importContactIntent, PICK_CONTACT);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
// Uri contactData = data.getData();
String id = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// System.out.println("name : " + name + ", ID : " + id);
// NOW query all numbers of that particulat contact using
// contact_Id
Cursor pCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
// you can store phone in a arrayList
destinationPhoneNumber = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//
enterPhoneNumber.setText(destinationPhoneNumber);
Toast.makeText(this,
name + " has number " + destinationPhoneNumber,
Toast.LENGTH_LONG).show();
cursor.close();
}
}
}
Here is the log cat error :
FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0i473.3217i217333/217328 (has extras) }} to activity {np.com.rsubedi.balancetransfer/np.com.rsubedi.balancetransfer.BTmain}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
at android.app.ActivityThread.access$2800(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at np.com.rsubedi.balancetransfer.BTmain.onActivityResult(BTmain.java:220)
at android.app.Activity.dispatchActivityResult(Activity.java:3890)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
... 11 more
Upvotes: 0
Views: 3980
Reputation: 2926
You have to first query the Contacts DB like this:
ContentResolver resolvr = getContentResolver();
Cursor cursor = resolvr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
The resultSet is in cursor
.
Upvotes: 1