【问题标题】:Android Lollipop contacts retrieving null pointer exceptionAndroid Lollipop 联系人检索空指针异常
【发布时间】:2015-05-14 09:40:39
【问题描述】:

在 Lollipop 设备上检索联系人电话号码时,光标 moveToFirst() 出现空指针异常,但在其他操作系统版本中它工作正常。

异常发生在pCur.moveToFirst(); under the getContact() method

请看我的代码:

public class MyService extends Service {

    public static Context mContext;
    LinkedHashMap<String, String> name = new LinkedHashMap<String, String>();
    HashMap<String, String> contactDetails = new HashMap<String, String>();
    HashMap<String, Bitmap> image = new HashMap<String, Bitmap>();
    private Cursor pCur, contactsCursor;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        String[] PROJECTION = { Contacts._ID, Contacts.LOOKUP_KEY,
                Contacts.DISPLAY_NAME_PRIMARY, Contacts.PHOTO_THUMBNAIL_URI,
                Contacts.SORT_KEY_PRIMARY };

        String SELECTION = Contacts.DISPLAY_NAME_PRIMARY + "<>''" + " AND "
                + Contacts.IN_VISIBLE_GROUP + "=1" + " AND "
                + Contacts.HAS_PHONE_NUMBER;
        String SORT_ORDER = Contacts.SORT_KEY_ALTERNATIVE;
        contactsCursor = getContentResolver().query(Contacts.CONTENT_URI,
                PROJECTION, SELECTION, null, SORT_ORDER);
        StoreCursor.qcursor = contactsCursor;
        Log.e("cur", "cur" + StoreCursor.qcursor.getCount());
        getContact();
        return START_STICKY;
    }

    private void getContact() {
        Log.e("result cursor", "" + contactsCursor.getCount());
        String displayName;
        String contactId;
        contactsCursor.moveToFirst();
        do {
            displayName = contactsCursor.getString(2);
            contactId = contactsCursor.getString(0);
            // Log.e("disName & id", displayName + "  "+contactId);
            pCur = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                    new String[] { contactId }, null);
            String cPN = "";

                pCur.moveToFirst(); // NullPointerException occur here.
                do {
                    int phoneType = pCur
                            .getInt(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    String phoneNumber = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    switch (phoneType) {
                    case Phone.TYPE_MOBILE:
                        cPN = cPN + "(mobile number)" + phoneNumber + "\n";
                        break;
                    case Phone.TYPE_HOME:
                        cPN = cPN + "(home number)" + phoneNumber + "\n";
                        break;
                    case Phone.TYPE_WORK:
                        cPN = cPN + "(work number)" + phoneNumber + "\n";
                        break;
                    case Phone.TYPE_OTHER:
                        cPN = cPN + "(other number)" + phoneNumber + "\n";
                        break;

                    default:
                        break;
                    }

                } while (pCur.moveToNext());
                name.put(contactId, displayName);
                Log.e("displayName", displayName);
                StoreCursor.name = name;
                contactDetails.put(contactId, cPN);
                StoreCursor.contactDetails = contactDetails;
                String photo = contactsCursor.getString(3) + "~";
                // Log.e("photo url", photo);
                if (photo.length() > 6) {
                    openPhoto(Long.valueOf(contactId), displayName);
                }

        } while (contactsCursor.moveToNext());

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

【问题讨论】:

    标签: android android-contentprovider android-5.0-lollipop android-contacts


    【解决方案1】:

    这很简单:

    如果pCur.moveToFirst(); 抛出NullPointerException,这意味着pCurnull。由于pCur只设置在一处,

    pCur = getContentResolver().query(...);
    

    这意味着您对ContentResolver.query 的调用将返回null

    现在真正的问题是:为什么ContentResolver.query 返回null?这是一个很好的问题,我建议您在 SO 上将其作为一个新问题提出。请务必包含所有相关信息,即:

    • 您要解析哪个 URL,
    • 您使用哪些参数以及从何处获取它们,

    理想情况下,您应该在新问题中添加minimal complete example(请注意,您当前的代码 sn-p 既不是最小的也不是完整的)。

    【讨论】:

    • 似乎选择和排序顺序查询在语法上不正确,这将产生不兼容的 SQL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多