2013-05-29 153 views
0

我在bulkInsert上收到nullPointerException。不知道爲什麼。 ContentValues似乎是正確的。我不確定是否將它正確地轉換爲CV數組。BulkInsert - ContentValues數組拋出nullPointerException異常

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

您的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); 
} 
+0

看起來不錯。明天早上我有機會嘗試。 – ono

+0

仍然nullPointer :( – ono

相關問題