Android Fetch Contact Information Returns Null Pointer Exception
In my android code I want to fetch contact's name,email and phone number as as json and then want to display. Here is my code: public class MainActivity extends Activity { pub
Solution 1:
You can use these approach to finds the contact from contact list
classFetchDeviceContactextendsAsyncTask<Void, Integer, String>
{
protectedvoidonPreExecute(){
Constant.showProgressDialog(AddDeviceContactScreeen.this);
}
protected String doInBackground(Void...arg0) {
arrayList.clear();
ContentResolvercr= AddDeviceContactScreeen.this.getContentResolver();
Cursorcur= AddDeviceContactScreeen.this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cur.moveToNext()) {
Stringid= cur.getString(cur.getColumnIndex(ContactsContract.Data._ID));
Stringname= cur.getString(cur.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
StringphotoUri= cur.getString(cur.getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI));
Bitmapmy_btmp= BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_up_blue);
Stringemail=null;
StringphoneNo=null;
Cursorphonecur= AddDeviceContactScreeen.this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
newString[]{id}, null);
if (photoUri != null) {
Urimy_contact_Uri= Uri.parse(photoUri);
try {
my_btmp = MediaStore.Images.Media.getBitmap(AddDeviceContactScreeen.this.getContentResolver(), my_contact_Uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (phonecur.getCount() > 0) {
while (phonecur.moveToNext()) {
phoneNo = phonecur.getString(phonecur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
CursoremailCur= cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
newString[]{id}, null);
while (emailCur.moveToNext()) {
// to get the contact names// = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)
email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if (email != null) {
System.out.println("Email============== :" + email);
}
}
emailCur.close();
ContactBeanbean=newContactBean();
bean.setName(name);
bean.setEmail(email);
bean.setImage(my_btmp);
bean.setPhone_number(phoneNo);
if (phoneNo == null || email == null) {
} else {
arrayList.add(bean);
}
}
return"";
}
protectedvoidonProgressUpdate(Integer...a){
}
protectedvoidonPostExecute(String result) {
Constant.cancelDialog();
}
}
newFetchDeviceContact().execute();
Create the getter and setter class for it:-
publicclassContactBean {
String name;
String email;
Bitmap image;
String phone_number;
publicStringgetName() {
return name;
}
publicvoidsetName(String name) {
this.name = name;
}
publicStringgetEmail() {
return email;
}
publicvoidsetEmail(String email) {
this.email = email;
}
publicBitmapgetImage() {
return image;
}
publicvoidsetImage(Bitmap image) {
this.image = image;
}
publicStringgetPhone_number() {
return phone_number;
}
publicvoidsetPhone_number(String phone_number) {
this.phone_number = phone_number;
}
}
Solution 2:
I think you should fix :
if (phoneCursor.movetoFirst()){
while (!phoneCursor.isAfterLast()) {
phoneNumber[p] = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
p++;
phoneCursor.movetoNext();
}
}
Solution 3:
Here:
phoneNumber[p] = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
NullPointerException
caused because phoneNumber
is null
and not initialized.
Use Cursor. getCount()
to initialize it as:
phoneNumber = newString[phoneCursor.getCount()];
phoneCursor.moveToFirst();
while (phoneCursor.moveToNext()) {
//..your code here..
}
Post a Comment for "Android Fetch Contact Information Returns Null Pointer Exception"