版本6.0上的Cursor.getString()null指针异常

时间:2016-08-27 04:57:27

标签: android nullpointerexception cursor

当我尝试通过电子邮件类型与设备联系时。它在光标上出现了空指针异常崩溃。为了解决这个问题,我已经添加了空检查。但是并没有给我很多帮助。

 private String getEmailsJsonString(String id, String label) {
    JSONArray emailJsonArray = new JSONArray();
    Cursor emailCursor = mContentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
        new String[]{ContactsContract.CommonDataKinds.Email.CONTACT_ID, ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.TYPE},
        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
        new String[]{id},
        null);
    if (emailCursor.getCount() > 0) {
        int emailColIdx = emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
        int email_type = emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE);

        while (emailCursor.moveToNext()) {
            String phone_label = null;
            if(email_type != 0 || email_type != -1 && emailCursor.getString(email_type) != null){
                switch (emailCursor.getString(email_type)) {
                    case "1":
                        phone_label = "Home";
                        break;
                    case "4":
                        phone_label = "Mobile";
                        break;
                    case "2":
                        phone_label = "Work";
                        break;
                    case "3":
                        phone_label = "Other";
                        break;
                    default:
                        phone_label = "Home";
                        break;
                }
            }
            emailJsonArray.put(HibeUtil.getEmailJsonObject(emailCursor.getString(emailColIdx), "add", phone_label));
        }
    }
    if (emailCursor != null) {
        emailCursor.close();
    }
    return emailJsonArray.toString();
}

CrashLogs:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
    at com.impressico.hibe.ui.minimvp.contacts.AddContactPresenter.getEmailsJsonString(AddContactPresenter.java:184)
    at com.impressico.hibe.ui.minimvp.contacts.AddContactPresenter.getContactFromCursor(AddContactPresenter.java:116)
    at com.impressico.hibe.ui.minimvp.contacts.AddContactPresenter.showDeviceContactList(AddContactPresenter.java:85)...............

0 个答案:

没有答案