java - Android: Get contact name using phone number -


i trying access contact keeps getting null pointer error.

nullpointerexception: attempt invoke virtual method ' android.content.contentresolver android.content.context.getcontentresolver()' on 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 how use

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 possible call broadcastreceiver in activity?

any appreciated. thanks

you didn't initialize value context in activity, , calling activity can call this,

contactname = getcontactname(this, number); 

or can set context value , call this,

context = this; contactname = getcontactname(context, number); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -