添加联系号码时出现NullPointer异常

时间:2015-01-18 17:27:19

标签: android nullpointerexception android-contacts

我想迭代联系人,然后向他们添加新号码。 ApplyBatch()中出现空指针异常。

这是我获取iD的代码

 protected Object doInBackground(Object[] objects) {
    final String[] projection = new String[] {
            RawContacts.CONTACT_ID, // the contact id column
            RawContacts.DELETED     // column if this contact is deleted
    };
    final Cursor rawContacts = context.getContentResolver().query(
            RawContacts.CONTENT_URI,    // the URI for raw contact provider
            projection,
            null,                    // selection = null, retrieve all entries
            null,                    // selection is without parameters
            null);                  // do not order
    final int contactIdColumnIndex = rawContacts.getColumnIndex(RawContacts.CONTACT_ID);

    final int deletedColumnIndex = rawContacts.getColumnIndex(RawContacts.DELETED);

    if(rawContacts.moveToFirst()) {
        while(!rawContacts.isAfterLast()) {     // still a valid entry left?
            final int contactId = rawContacts.getInt(contactIdColumnIndex);
            final boolean deleted = (rawContacts.getInt(deletedColumnIndex) == 1);
            if(!deleted) {
                EditContactPhone(contactId);
            }
            rawContacts.moveToNext();           // move to the next entry
        }
    }
    rawContacts.close();
    if (!ops.isEmpty()) {
        try {
            context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        }
    }
    return null;
}

这是EditContactPhone方法,它将数据添加到操作

private void EditContactPhone(int contactId) {
    final String[] projection = new String[] {
            Phone.NUMBER,
            Phone.TYPE
    };
    final Cursor phone =  context.getContentResolver().query(
            Phone.CONTENT_URI,
            projection,
            Data.CONTACT_ID + "=?",
            new String[]{String.valueOf(contactId)},
            null);
    if(phone.moveToFirst()) {
        while (!phone.isAfterLast()) {
            String newNumber = buildNineFineNine(number); //Function which modify the NewNumber to be added
            ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                            .withValue(ContactsContract.Data.CONTACT_ID, contactId)
                            .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                            .withValue(Phone.NUMBER, newNumber)
                            .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
                            .withValue(Phone.LABEL, "NineFiveNine")
                            .build());
                    phone.moveToNext();
        }
    }
    phone.close();
}

当我将ContactsContract.Data.CONTACT_ID切换为RAW_CONTACT_ID时,它成功完成但数字被添加到错误的联系人中。我仍然无法弄清楚为什么它会向我显示nullPointer Exception。

这是LOGCAT

01-18 23:46:02.955  15824-15955/vapp.ninefivenineconverter E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
        at java.util.concurrent.FutureTask.run(FutureTask.java:239)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:838)
 Caused by: java.lang.NullPointerException
        at android.os.Parcel.readException(Parcel.java:1434)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
        at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:160)
        at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:461)
        at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:225)
        at android.content.ContentResolver.applyBatch(ContentResolver.java:976)
        at vapp.ninefivenineconverter.ConverterTask.doInBackground(ConverterTask.java:67)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)

1 个答案:

答案 0 :(得分:0)

第一个问题可能在这里:

 .withValue(Phone.NUMBER, newNumber)

确保 newNumber 不是空的。

如果这没有帮助,问题仍然存在,请尝试使用URI:

ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);

而不是

ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);

接下来的工作就是简单地使用Data.CONTENT_URI和RAW_CONTACT_ID:

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValue(ContactsContract.Data.RAW_CONTACT_ID, id)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumber)
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
                    .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, "NineFiveNine").build());