从Android设备错误中获取所有联系人

时间:2014-03-01 07:06:14

标签: java android listview android-intent

使用phonegap构建一个启动后台服务的应用程序,并在后台服务中调用一个以json格式提取所有用户联系人的方法。

以下是我测试的代码:

  http://code.tutsplus.com/tutorials/build-a-contacts-application-with-jquery-mobile-the-android-sdk-part-1--mobile-5727

以下代码:

    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();               
}

从另一个类调用时:

    protected JSONObject initialiseLatestResult() {
    // TODO Auto-generated method stub
    final String ContactData = ContactUtility.getAllContactDisplaysJSON(getContentResolver());
    return null;
}

我在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

Android(Java)新手,所以我真的不明白发生了什么

1 个答案:

答案 0 :(得分:1)

您可以通过我的代码获取您的联系人姓名和电话号码,如果您想获取图片然后告诉您可以将所有数据保存到对象arraylist并显示到自定义适配器

 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) {

                }