0

我在 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);
    }
4

1 回答 1

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);
}
于 2013-05-29T22:56:17.063 回答