Reputation: 1157
Am building an app with phonegap which starts a background service, and in the background service a method is called which fetches all the users contact in json format.
Here is the code am testing with which was gotten from:
http://code.tutsplus.com/tutorials/build-a-contacts-application-with-jquery-mobile-the-android-sdk-part-1--mobile-5727
Code Below:
public static String getAllContactDisplaysJSON(ContentResolver contentResolver){
// Obtain all ContactDisplay objects from database and sort them
ArrayList<ContactDisplay> list = getAllContactDisplays(contentResolver);
Collections.sort(list);
// Populate the data structure consisting of ContactList-ContactGroup-ContactDisplay objects
// Start with initializing some variables
ContactList contactList = new ContactList();
String key = "";
ArrayList<ContactDisplay> values = new ArrayList<ContactDisplay>();
ContactGroup group = null;
StringWriter writer = new StringWriter();
// Process the list ContactDisplay objects to construct the data structure
if(list != null && !list.isEmpty()){
for(ContactDisplay display:list){
if(!display.getKey().equals(key)){
if(values.size() > 0){
group = new ContactGroup();
group.setKey(key);
group.setValues(values);
contactList.getContacts().add(group);
}
key = display.getKey();
values = new ArrayList<ContactDisplay>();
}
values.add(display);
}
// Add the last group
if(values.size() > 0){
group = new ContactGroup();
group.setKey(key);
group.setValues(values);
contactList.getContacts().add(group);
}
}else{
return EMPTY_CONTACT_LIST;
}
// We have the data structure of ContactList-ContactGroup-ContactDisplay objects where
// contactList is the root level element. Write it to a JSON formatted string using
// org.codehaus.jackson.map.ObjectMapper.
try{
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(writer, contactList);
}catch(Exception e){
return EMPTY_CONTACT_LIST;
}
return writer.toString();
}
when called from another class like so :
protected JSONObject initialiseLatestResult() {
// TODO Auto-generated method stub
final String ContactData = ContactUtility.getAllContactDisplaysJSON(getContentResolver());
return null;
}
I get the following error in logcat :
03-01 07:55:25.289: E/AndroidRuntime(11022): FATAL EXCEPTION: main
03-01 07:55:25.289: E/AndroidRuntime(11022): java.lang.RuntimeException: Unable to create service com.mycontacts.MyService: java.lang.NullPointerException
03-01 07:55:25.289: E/AndroidRuntime(11022): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1966)
03-01 07:55:25.289: E/AndroidRuntime(11022): at android.app.ActivityThread.access$2500(ActivityThread.java:117)
03-01 07:55:25.289: E/AndroidRuntime(11022): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:996)
03-01 07:55:25.289: E/AndroidRuntime(11022): at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 07:55:25.289: E/AndroidRuntime(11022): at android.os.Looper.loop(Looper.java:130)
03-01 07:55:25.289: E/AndroidRuntime(11022): at android.app.ActivityThread.main(ActivityThread.java:3735)
03-01 07:55:25.289: E/AndroidRuntime(11022): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 07:55:25.289: E/AndroidRuntime(11022): at java.lang.reflect.Method.invoke(Method.java:507)
03-01 07:55:25.289: E/AndroidRuntime(11022): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
03-01 07:55:25.289: E/AndroidRuntime(11022): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
03-01 07:55:25.289: E/AndroidRuntime(11022): at dalvik.system.NativeStart.main(Native Method)
03-01 07:55:25.289: E/AndroidRuntime(11022): Caused by: java.lang.NullPointerException
03-01 07:55:25.289: E/AndroidRuntime(11022): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:90)
03-01 07:55:25.289: E/AndroidRuntime(11022): at com.mycontacts.NativeServices.SendPushMsg(NativeServices.java:68)
03-01 07:55:25.289: E/AndroidRuntime(11022): at com.mycontacts.MyService.initialiseLatestResult(MyService.java:63)
03-01 07:55:25.289: E/AndroidRuntime(11022): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.initialiseService(BackgroundService.java:299)
03-01 07:55:25.289: E/AndroidRuntime(11022): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.onCreate(BackgroundService.java:129)
03-01 07:55:25.289: E/AndroidRuntime(11022): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1956)
03-01 07:55:25.289: E/AndroidRuntime(11022): ... 10 more
03-01 07:55:25.299: E/AndroidRuntime(11022): [Blue Error Handler] Make Debugging Report file for main
03-01 07:55:25.299: E/AndroidRuntime(11022): java.lang.RuntimeException: Unable to create service com.mycontacts.MyService: java.lang.NullPointerException
03-01 07:55:25.299: E/AndroidRuntime(11022): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1966)
03-01 07:55:25.299: E/AndroidRuntime(11022): at android.app.ActivityThread.access$2500(ActivityThread.java:117)
03-01 07:55:25.299: E/AndroidRuntime(11022): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:996)
03-01 07:55:25.299: E/AndroidRuntime(11022): at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 07:55:25.299: E/AndroidRuntime(11022): at android.os.Looper.loop(Looper.java:130)
03-01 07:55:25.299: E/AndroidRuntime(11022): at android.app.ActivityThread.main(ActivityThread.java:3735)
03-01 07:55:25.299: E/AndroidRuntime(11022): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 07:55:25.299: E/AndroidRuntime(11022): at java.lang.reflect.Method.invoke(Method.java:507)
03-01 07:55:25.299: E/AndroidRuntime(11022): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
03-01 07:55:25.299: E/AndroidRuntime(11022): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
03-01 07:55:25.299: E/AndroidRuntime(11022): at dalvik.system.NativeStart.main(Native Method)
03-01 07:55:25.299: E/AndroidRuntime(11022): Caused by: java.lang.NullPointerException
03-01 07:55:25.299: E/AndroidRuntime(11022): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:90)
03-01 07:55:25.299: E/AndroidRuntime(11022): at com.mycontacts.NativeServices.SendPushMsg(NativeServices.java:68)
03-01 07:55:25.299: E/AndroidRuntime(11022): at com.mycontacts.MyService.initialiseLatestResult(MyService.java:63)
03-01 07:55:25.299: E/AndroidRuntime(11022): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.initialiseService(BackgroundService.java:299)
03-01 07:55:25.299: E/AndroidRuntime(11022): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.onCreate(BackgroundService.java:129)
03-01 07:55:25.299: E/AndroidRuntime(11022): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1956)
03-01 07:55:25.299: E/AndroidRuntime(11022): ... 10 more
03-01 07:55:32.529: I/BackgroundService(11063): Service creating
03-01 07:55:32.529: I/BackgroundService(11063): Initialising the service
03-01 07:55:32.529: W/dalvikvm(11063): threadid=1: thread exiting with uncaught exception (group=0x4017f560)
03-01 07:55:32.529: E/AndroidRuntime(11063): FATAL EXCEPTION: main
03-01 07:55:32.529: E/AndroidRuntime(11063): java.lang.RuntimeException: Unable to create service com.mycontacts.MyService: java.lang.NullPointerException
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1966)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.app.ActivityThread.access$2500(ActivityThread.java:117)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:996)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.os.Looper.loop(Looper.java:130)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.app.ActivityThread.main(ActivityThread.java:3735)
03-01 07:55:32.529: E/AndroidRuntime(11063): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 07:55:32.529: E/AndroidRuntime(11063): at java.lang.reflect.Method.invoke(Method.java:507)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
03-01 07:55:32.529: E/AndroidRuntime(11063): at dalvik.system.NativeStart.main(Native Method)
03-01 07:55:32.529: E/AndroidRuntime(11063): Caused by: java.lang.NullPointerException
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:90)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.mycontacts.NativeServices.SendPushMsg(NativeServices.java:68)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.mycontacts.MyService.initialiseLatestResult(MyService.java:63)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.initialiseService(BackgroundService.java:299)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.onCreate(BackgroundService.java:129)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1956)
03-01 07:55:32.529: E/AndroidRuntime(11063): ... 10 more
03-01 07:55:32.529: E/AndroidRuntime(11063): [Blue Error Handler] Make Debugging Report file for main
03-01 07:55:32.529: E/AndroidRuntime(11063): java.lang.RuntimeException: Unable to create service com.mycontacts.MyService: java.lang.NullPointerException
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1966)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.app.ActivityThread.access$2500(ActivityThread.java:117)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:996)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.os.Looper.loop(Looper.java:130)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.app.ActivityThread.main(ActivityThread.java:3735)
03-01 07:55:32.529: E/AndroidRuntime(11063): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 07:55:32.529: E/AndroidRuntime(11063): at java.lang.reflect.Method.invoke(Method.java:507)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
03-01 07:55:32.529: E/AndroidRuntime(11063): at dalvik.system.NativeStart.main(Native Method)
03-01 07:55:32.529: E/AndroidRuntime(11063): Caused by: java.lang.NullPointerException
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:90)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.mycontacts.NativeServices.SendPushMsg(NativeServices.java:68)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.mycontacts.MyService.initialiseLatestResult(MyService.java:63)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.initialiseService(BackgroundService.java:299)
03-01 07:55:32.529: E/AndroidRuntime(11063): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.onCreate(BackgroundService.java:129)
03-01 07:55:32.529: E/AndroidRuntime(11063): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1956)
03-01 07:55:32.529: E/AndroidRuntime(11063): ... 10 more
Am New to Android(Java) so i don't really understand what is happening
Upvotes: 0
Views: 105
Reputation: 258
you can fetch your contact name and phone number through my code and if you want to fetch image then tell and you can save all data into object arraylist and can show into custom adapter
try {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
while (phones.moveToNext()) {
//you can define String name and phone number globally use
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.d("NAME", name);
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
phones.close();
} catch (Exception e) {
}
Upvotes: 1