Search code examples
androidandroid-contentproviderandroid-contactssamsung-mobilecontactscontract

Adding a contact group gives NullPointerexception on HTC and Samsung devices


I'm trying to add a contacts group with a ContentProviderOperation. This works on Nexus, Moto G and Asus devices but it crashes on HTC M7, M8, M8 mini and M9, Samsung S4, S5. Because I only have the sources of the normal clean Android I can't find what the Nullpointer is causing by debugging.

None of the values I use in this ContentProviderOperation are null. The groups are added to a custom account type specified in authenticator.xml.

Code:

public Uri addContactGroup(String title, boolean autoAdd, boolean isReadOnly, boolean isVisible){
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();

    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Groups.CONTENT_URI)
            .withValue(ContactsContract.Groups.TITLE, title)
            .withValue(ContactsContract.Groups.ACCOUNT_TYPE, getAccount().type)
            .withValue(ContactsContract.Groups.ACCOUNT_NAME, getAccount().name)
            .withValue(ContactsContract.Groups.AUTO_ADD, autoAdd)
            .withValue(ContactsContract.Groups.GROUP_IS_READ_ONLY, isReadOnly)
            .withValue(ContactsContract.Groups.GROUP_VISIBLE, isVisible)
            .build());
    try {
        ContentProviderResult[] result = mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        if(result.length > 0) {
            return result[0].uri;
        }
    } catch (RemoteException | OperationApplicationException e){
        Log.e(TAG, e.toString());
    }
    return null;
}

Throws this exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference at android.os.Parcel.readException(Parcel.java:1555) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185) at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:160) at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:573) at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:377) at android.content.ContentResolver.applyBatch(ContentResolver.java:1265) at nl.dalvikvm.stackoverflowquestion.content.proces.CardProcess.addContactGroup(CardProcess.java:284) at nl.dalvikvm.stackoverflowquestion.content.proces.CardProcess.initGroups(CardProcess.java:244) at nl.dalvikvm.stackoverflowquestion.sync.SyncUtils.CreateSyncAccount(SyncUtils.java:55) at nl.dalvikvm.stackoverflowquestion.ui.MainActivity$1.run(MainActivity.java:288) at java.lang.Thread.run(Thread.java:818)


Solution

  • Finally found the solution!

            .withValue(ContactsContract.Groups.AUTO_ADD, autoAdd ? 1 : 0)
            .withValue(ContactsContract.Groups.GROUP_IS_READ_ONLY, isReadOnly ? 1 : 0)
            .withValue(ContactsContract.Groups.GROUP_VISIBLE, isVisible ? 1 : 0)
    

    Apparently on Samsung and HTC devices boolean values are not accepted as values and they must be converted to integers first. I have no idea why it worked on the other devices.