使用applyBatch()更新ContactsContract.Data.CONTENT_URI表时获取空指针异常

时间:2015-06-23 07:30:27

标签: android

任何人都可以帮我找到为什么会出现以下错误? 我正在尝试使用applyBatch()方法从表ContactsContract.Data更新电话号码和电子邮件地址。

请查找错误日志

tactsDatabaseHelper( 1403): invalidateAllCache: [SemcContactsDatabaseHelper]
E/DatabaseUtils( 1403): Writing exception to parcel
E/DatabaseUtils( 1403): java.lang.NullPointerException
E/DatabaseUtils( 1403):     at com.android.providers.contacts.NameNormalizer.lettersAndDigitsOnly(NameNormalizer.java:107)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.NameNormalizer.compareComplexity(NameNormalizer.java:88)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.aggregation.ContactAggregator.processDisplayNameCandidate(ContactAggregator.java:2013)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.aggregation.ContactAggregator.updateDisplayNameForContact(ContactAggregator.java:2217)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.aggregation.ContactAggregator.updateDisplayNameForRawContact(ContactAggregator.java:2198)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.DataRowHandler.fixRawContactDisplayName(DataRowHandler.java:308)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.DataRowHandlerForStructuredName.update(DataRowHandlerForStructuredName.java:92)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.ContactsProvider2.updateData(ContactsProvider2.java:4356)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.ContactsProvider2.updateData(ContactsProvider2.java:4338)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.ContactsProvider2.updateInTransaction(ContactsProvider2.java:3793)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.SemcContactsProvider2.updateInTransaction(SemcContactsProvider2.java:970)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.AbstractContactsProvider.update(AbstractContactsProvider.java:166)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.ContactsProvider2.update(ContactsProvider2.java:2006)
E/DatabaseUtils( 1403):     at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:225)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.AbstractContactsProvider.applyBatch(AbstractContactsProvider.java:237)
E/DatabaseUtils( 1403):     at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2118)
E/DatabaseUtils( 1403):     at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:222)
E/DatabaseUtils( 1403):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:183)
E/DatabaseUtils( 1403):     at android.os.Binder.execTransact(Binder.java:351)
E/DatabaseUtils( 1403):     at dalvik.system.NativeStart.run(Native Method)
W/System.err(28538): java.lang.NullPointerException
W/System.err(28538):    at android.os.Parcel.readException(Parcel.java:1431)
W/System.err(28538):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
W/System.err(28538):    at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:160)
W/System.err(28538):    at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:469)
W/System.err(28538):    at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:225)
W/System.err(28538):    at android.content.ContentResolver.applyBatch(ContentResolver.java:909)

这是我的代码,我尝试运行:

ArrayList<ContentProviderOperation> valueArrayToUpdate = new ArrayList<ContentProviderOperation>();
ArrayList<ContentProviderOperation> valueArrayToDeleteFromData = new ArrayList<ContentProviderOperation>();
private int updateAndDelete(ContentResolver contentResolver, String replacement, long id) {
    int res = 0;
    Uri contactsUri = ContactsContract.Data.CONTENT_URI;
    try {

        if(null != valueArrayToUpdate) {
            if(valueArrayToUpdate.size() >= 20){
                int resBatch = applyBatchToDelete(contentResolver,valueArrayToUpdate);

                if(resBatch > 0) {
                    applyBatchToDelete(contentResolver,valueArrayToDeleteFromData);

                }
                valueArrayToDelete.clear();
                valueArrayToDeleteFromData.clear();

            }else {
                ContentValues values = new ContentValues();
                values.put(ContactsContract.Data.DATA1, replacement);
                values.put(Email.ADDRESS, replacement);
                String whereClause = Data.CONTACT_ID +" = ?";
                valueArrayToUpdate.add(ContentProviderOperation.newUpdate(contactsUri)
                        .withSelection(whereClause, new String[]{Long.toString(id)})
                        .withYieldAllowed(true)
                        .withValues(values).build());
                valueArrayToDeleteFromData.add(ContentProviderOperation.newDelete(contactsUri)
                        .withSelection(whereClause, new String[]{Long.toString(id)})
                        .withYieldAllowed(true)
                        .build());

            }
        }

    } catch (Exception e) {
        e.printstacktrace();
    }
    return res;
}


private int applyBatchToDelete(ContentResolver contentResolver, ArrayList valueArrayToDelete){

        int res =0;
        if(valueArrayToDelete == null ){                
            return res;
        }
        if(null != valueArrayToDelete && valueArrayToDelete.size()>0) {
            try {
                ContentProviderResult[] resArray = contentResolver.applyBatch(ContactsContract.AUTHORITY, valueArrayToDelete);
                if (null != resArray) {
                    res = resArray.length;
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                valueArrayToDelete.clear();
            }
        }
        return  res;
}

1 个答案:

答案 0 :(得分:0)

以下是发生零点异常的原因。 请仔细查看代码有什么问题。

  
      
  1. 在null对象上调用instance方法时。如果在null对象上调用静态方法或类方法,则不会得到空指针异常,因为静态方法不需要实例来调用任何方法。

  2.   
  3. 访问或更改null对象上的任何变量或字段时。

  4.   
  5. 当预计会抛出异常时抛出null。

  6.   
  7. 当数组为空时调用数组的长度。

  8.   
  9. 像数组一样访问或更改null的插槽。

  10.   
  11. 尝试在null对象上进行同步或在Java中的synchronized块内使用null

  12.