我在bulkInsert上遇到nullPointerException。不知道为什么。 ContentValues似乎是正确的。我不确定我是否正确地将它转换为CV的数组。
if (contactList != null && !contactList.isEmpty()) {
ContentValues cv = new ContentValues();
List<ContentValues>valueList = new ArrayList<ContentValues>();
ContentValues[] vc = new ContentValues[contactList.size()];
for (HashMap<String, Object> userDict : contactList) {
progress.setProgress(contactsLoaded);
// Initialize values we're looking for. In this case, cn (common name), sn (surname), alias, sipExt.
String cn = userDict.get("cn").toString();
String sn = userDict.get("sn").toString();
String alias = userDict.get("username").toString();
String sipExt = userDict.get("sipExt").toString();
cv = new ContentValues();
cv.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
cv.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, cn);
cv.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, sn);
cv.put(ContactsContract.CommonDataKinds.StructuredName.PREFIX, alias);
cv.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, sipExt);
valueList.add(cv);
contactsLoaded++;
}
System.out.println("CV: "+valueList);
vc = valueList.toArray(vc);
System.out.println("VC: "+vc);
myActivity.getContentResolver().bulkInsert(ContactsContract.Data.CONTENT_URI, vc);
}
答案 0 :(得分:0)
您的cv变量不是必需的,因为您已经知道数组的大小。试试这个:
if (contactList != null && !contactList.isEmpty()) {
ContentValues[] cv = new ContentValues[contactList.size()];
for (int i = 0; i < contactList.size(); i++) {
HashMap<String, Object> userDict = contactList.get(i);
progress.setProgress(contactsLoaded);
// Initialize values we're looking for. In this case, cn (common name), sn (surname), alias, sipExt.
String cn = userDict.get("cn").toString();
String sn = userDict.get("sn").toString();
String alias = userDict.get("username").toString();
String sipExt = userDict.get("sipExt").toString();
ContentValues contentValues = new ContentValues();
contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, cn);
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, sn);
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.PREFIX, alias);
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, sipExt);
cv[i] = contentValues;
contactsLoaded++;
}
myActivity.getContentResolver().bulkInsert(ContactsContract.Data.CONTENT_URI, cv);
}