2

I've I'm faced to a big issue : I can't add datas to an existing contact :( I'm developping an application wich manage contacts, I can add, delete or edit contacts. The creation is ok, because I create all types of datas (even if they are empty) for the contact (this is NOT a good way, I will change this later). So when I want to edit those datas I can find them to the data base (with Datas.CONTENT_URI) 'cause they allready exist !

But if I create a new contact with the ANDROID contact application only the fill fatas are created for a contact. When I try to add new phone number for example with my application I get a error :(

Here is my code to try to add phone number :

ContentValues contentValues = new ContentValues();

contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, pNewPhoneNumber.number);
contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, pNewPhoneNumber.type);
contentValues.put(ContactsContract.Data.CONTACT_ID, pContact.getContactId());

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation.newInsert(
  ContactsContract.Data.CONTENT_URI).withValues(contentValues).build());

try
{
 pContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} 
catch (Exception e)
{
        Context ctx = pContext.getApplicationContext();
        String txt = "Exception encoutered while inserting contact: " + e;
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(ctx, txt, duration);
        toast.show();
 Log.e(TAG, txt);
}

Here is the error :

06-23 09:21:54.030: ERROR/DatabaseUtils(111): Writing exception to parcel
06-23 09:21:54.030: ERROR/DatabaseUtils(111): java.lang.NullPointerException
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2206)
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2096)
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:101)
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:1941)
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:211)
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:200)
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:1960)
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:169)
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:167)
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at android.os.Binder.execTransact(Binder.java:287)
06-23 09:21:54.030: ERROR/DatabaseUtils(111):     at dalvik.system.NativeStart.run(Native Method)

So I have a database problem, OK. But where am I wrong ?

If you know how to solve this problem : let me know :) Thank you

Destra
  • 63
  • 1
  • 6

2 Answers2

5

I guess

contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, ...);

is missing.

Gawcio
  • 1,125
  • 1
  • 9
  • 17
4

Thank you very much, it works perfectly. I have to get the rawid of the contact and then use your solution.

Here is the working source code :

    ContentValues contentValues = new ContentValues();

    ContentValues contentValues = new ContentValues();
    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, pContact.getContactRawId());
    contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, pNewPhoneNumber.number);
    contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, pNewPhoneNumber.type);

    ops.add(ContentProviderOperation.newInsert(
            ContactsContract.Data.CONTENT_URI).withValues(contentValues).build()); 

Thank you again ;)

Destra
  • 63
  • 1
  • 6