当我得到联系人时它会例外吗?

时间:2012-06-15 15:07:47

标签: android android-intent android-contacts

我要阅读5-联系人,所以我得到这个代码并顺利进行,没有发生错误, 但是,当我添加这两行以保存名称和联系人以后使用它们时,出现了这个异常!!

这是代码:

    Cursor cursor = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
        name = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        // You know it has a number so now query it like this
        Cursor phones1 = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                        + contactId, null, null);
        if (count > 5)
            break;
        while (phones1.moveToNext()) {
            phoneNumber = phones1
                    .getString(phones1
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


        }

        phones1.close();


        Toast.makeText(this,
                contactId + " __ " + name + " # :  " + phoneNumber,
                Toast.LENGTH_SHORT).show();
        Contact2[count] = name;
        Numbers2[count] = phoneNumber;
        count++;

    }

这两个陈述导致异常!!

Contact2[count] = name;
Numbers2[count] = phoneNumber;

然而,如果没有这些陈述,代码就会顺利进行。

这是logcat窗口

06-15 18:00:02.745: I/ApplicationPackageManager(8639): cscCountry is not German : XEU
06-15 18:00:03.586: W/dalvikvm(8639): threadid=1: thread exiting with uncaught   exception (group=0x4001d560)
06-15 18:00:03.586: E/AndroidRuntime(8639): FATAL EXCEPTION: main
06-15 18:00:03.586: E/AndroidRuntime(8639): java.lang.IllegalStateException: Could not  execute method of the activity
06-15 18:00:03.586: E/AndroidRuntime(8639):     at  android.view.View$1.onClick(View.java:2154)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at android.view.View.performClick(View.java:2538)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at android.view.View$PerformClick.run(View.java:9152)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at android.os.Handler.handleCallback(Handler.java:587) 
06-15 18:00:03.586: E/AndroidRuntime(8639):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at android.os.Looper.loop(Looper.java:123)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at java.lang.reflect.Method.invokeNative(Native Method)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at java.lang.reflect.Method.invoke(Method.java:507)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at dalvik.system.NativeStart.main(Native Method)
06-15 18:00:03.586: E/AndroidRuntime(8639): Caused by: java.lang.reflect.InvocationTargetException
06-15 18:00:03.586: E/AndroidRuntime(8639):     at java.lang.reflect.Method.invokeNative(Native Method)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at java.lang.reflect.Method.invoke(Method.java:507)
06-15 18:00:03.586: E/AndroidRuntime(8639):     at android.view.View$1.onClick(View.java:2149)
06-15 18:00:03.586: E/AndroidRuntime(8639):     ... 11 more
06-15 18:00:03.586: E/AndroidRuntime(8639): Caused by: java.lang.NullPointerException
06-15 18:00:03.586: E/AndroidRuntime(8639):     at oo.k.TestActivity.upload(TestActivity.java:156)

我只是需要保存联系人结果。

1 个答案:

答案 0 :(得分:0)

看起来您正在获得Null指针异常,可能是因为您尚未初始化数组Contact2Numbers2

Contact2 = new String[5];