在Lollipop设备上检索联系人电话号码时,在光标moveToFirst()处出现空指针异常,但在其他OS版本中,它工作正常。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();
}
}
最佳答案
这很简单:
如果pCur.moveToFirst();
抛出NullPointerException,则意味着pCur
是null
。由于pCur
仅设置在一个位置,
pCur = getContentResolver().query(...);
这意味着您对
ContentResolver.query
的调用将返回null
。现在真正的问题是:为什么
ContentResolver.query
返回null
?这是一个很好的问题,我建议您在SO上作为一个新问题提出。确保包括所有相关信息,即:您要解析哪个URL,
您使用哪些参数以及从何处获取参数,
等等
理想情况下,您应该在新问题中添加minimal complete example(请注意,您当前的代码段既不是最小也不是完整的)。