简体   繁体   中英

Android: Get contact name using phone number

I am trying to access contact but keeps getting null pointer error.

NullPointerException: Attempt to invoke virtual method ' android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference

Code

 public static String getContactName(Context context, String phoneNumber) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = context.getContentResolver().query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    String contactNumber = "";
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    return contactNumber.equals("") ? phoneNumber : contactName;
}

Here is how I use it

public class VideoActivity extends Activity {

String contactName;
String phoneNumber;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);

    //Set caller phone
    String number = getIntent().getStringExtra(
            TelephonyManager.EXTRA_INCOMING_NUMBER);

   contactName = getContactName(context, number);
    TextView text = (TextView) findViewById(R.id.textView2);
    text.setText(contactName);

}

Is it possible to call BroadcastReceiver in an activity?

Any help will be appreciated. Thanks

You didn't initialize the value context in your activity, and you are calling from activity so you can call like this,

contactName = getContactName(this, number);

or you can set the context value and call like this,

context = this;
contactName = getContactName(context, number);
private void ContactList(){
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
        Cursor cursor = context.getContentResolver().query(uri, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}, selection, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");


        cursor.moveToFirst();
        while (cursor.isAfterLast() == false) {

            String contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            }
            cursor.moveToNext();
        }
        cursor.close();
        cursor = null;

      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM