4

I have an app that can add and delete contacts just fine. And if there is an existing value in an existing contact, it can be modified just fine. But I can't seem to be able to insert new values into existing contacts. For example, if there is an existing value for a home phone number, but not for the work phone number, I tried using the fillowing to add the value (cintact idValue and workNumber are passed in):

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValue(ContactsContract.Data.CONTACT_ID, idValue)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber)
            .build());
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

But I get a NullPointerException:

java.lang.NullPointerException
    at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2604)
    at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2452)
    at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
    at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2256)
    at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
    at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
    at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2290)
    at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:217)
    at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:194)
    at android.os.Binder.execTransact(Binder.java:336)
    at dalvik.system.NativeStart.run(Native Method)

Can someone please tell me what I'm doing wrong?

user496854
  • 6,461
  • 10
  • 47
  • 84

2 Answers2

6

You are missing withValue(Phone.TYPE, Phone.TYPE_WORK)

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValue(ContactsContract.Data.RAW_CONTACT_ID, idValue)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber).
withValue(Phone.TYPE, Phone.TYPE_WORK)
        .build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

Edits

You are correct I checked the schema for the data table and it is as follow it is only using raw contacts id and not contact id

CREATE TABLE data (_id INTEGER PRIMARY KEY AUTOINCREMENT,
package_id INTEGER REFERENCES package(_id),
mimetype_id INTEGER REFERENCES mimetype(_id) NOT NULL,
raw_contact_id INTEGER REFERENCES raw_contacts(_id) NOT NULL,
is_primary INTEGER NOT NULL DEFAULT 0,
is_super_primary INTEGER NOT NULL DEFAULT 0,
data_version INTEGER NOT NULL DEFAULT 0,
data1 TEXT,
data2 TEXT,
data3 TEXT,
data4 TEXT,
data5 TEXT,
data6 TEXT,
data7 TEXT,
data8 TEXT,
data9 TEXT,
data10 TEXT,
data11 TEXT,
data12 TEXT,
data13 TEXT,
data14 TEXT,
data15 TEXT,
data_sync1 TEXT, 
data_sync2 TEXT, 
data_sync3 TEXT, 
data_sync4 TEXT );
Vivek
  • 4,170
  • 6
  • 36
  • 50
  • Thanks, that was bone-headed of me, but that wasn't causing the problem. I've updated the code in the original post, but still getting the error. – user496854 Oct 28 '11 at 05:52
  • It seems to be now your problem in not on this lines. – Vivek Oct 28 '11 at 06:07
  • What do you mean by that? The code is trhowing an exception -- how can it be a problem on my end? – user496854 Oct 28 '11 at 06:24
  • It means that I have did same code at my end it is working fine. Debug the code or put the log and find where exactly error is coming. – Vivek Oct 28 '11 at 07:10
  • Thanks for your help -- I found the problem, and for some reason it wasn't finding the contact by CONTACT_ID, but wanted RAW_CONTACT_ID instead. I really want to accept your answer and give you the points, but I can't do that with the way the current one shows up. Can you please change it to say ".withValue(ContactsContract.Data.RAW_CONTACT_ID, idValue)", and then I'll accept it – user496854 Oct 28 '11 at 18:10
  • I have used `Data.RAW_CONTACT_ID`. It's my mistake that I haven't checked it carefully. – Vivek Oct 29 '11 at 11:50
1

You were getting error because Android doesn't give you access to add a contact directly to you Contact table. Rather you need to modify or add a raw Contact and Android will automatically create a Contact for you.

Ali Hesari
  • 1,821
  • 5
  • 25
  • 51