0

我的应用程序必须更新给定联系人的组织属性。如果联系人已经填充了组织属性,则此方法可以正常工作,但如果组织标记为空,则返回 NullPointerException。

public boolean updateOrganizationFromContact(String contactId, String companyName){

//Get contact from ContactList
    boolean foundContact = false;
    int i = -1;
    while(!foundContact && i<contactList.size()){
        i++;
        foundItem = contactId.equals(contactList.get(i).getId());
    }
//Check if Contact has already a filled up organization-tag
    if (contactList.get(i).getOrganization().hasEntry()){
//if yes, update:
        ContentResolver contentResolver = context.getContentResolver();
        String where = ContactsContract.Data.CONTACT_ID + " = ? AND " +
                       ContactsContract.Data.MIMETYPE + " = ?";
        String[] whereParameters = new String[]{contactId, 
            ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
        ContentValues values = new ContentValues();
            values.put(ContactsContract.CommonDataKinds.Organization.DATA, companyName);
        contentResolver.update(ContactsContract.Data.CONTENT_URI, values, where, whereParameters);
        return true;
    } else {
//if not, go to next method:
        return createOrganizationFromContact(contactId, companyName);
    }
}

private boolean createOrganizationFromContact(String contactId, String companyName) {
    ContentResolver contentResolver = context.getContentResolver();
    ContentValues values = new ContentValues();
    values.put(ContactsContract.Data.CONTACT_ID, contactId);
    values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
    values.put(ContactsContract.CommonDataKinds.Organization.DATA, companyName);

//now this is the line with the NPE (line 212):
    Uri uri = contentResolver.insert(ContactsContract.Data.CONTENT_URI, values);

    return true;
}

结果堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/1229i......... flg=0x1 }} 
              to activity {Activity}: java.lang.NullPointerException
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
        at android.app.ActivityThread.access$1100(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at android.os.Parcel.readException(Parcel.java:1431)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
        at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
        at android.content.ContentResolver.insert(ContentResolver.java:866)
        at de.MYAPPLICTION.dienstleistung.objects.ContactAPI.ContactInformationSingleton.createOrganizationFromContact(ContactInformationSingleton.java:212)
        at de.MYAPPLICTION.dienstleistung.objects.ContactAPI.ContactInformationSingleton.updateOrganizationFromContact(ContactInformationSingleton.java:202)
        at de.MYAPPLICTION.fragments.client.NewClientActivity.addContactToClient(NewClientActivity.java:282)
        at de.MYAPPLICTION.dienstleistung.fragments.client.NewClientActivity.onActivityResult(NewClientActivity.java:274)
        at android.app.Activity.dispatchActivityResult(Activity.java:5293)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
        ... 11 more

我认为缺少一些 ContentValues。但我找不到任何用于更新联系人组织的文档/示例。有什么帮助吗?

4

1 回答 1

0

发现我对 RawContacts 的理解很差。您必须先创建 RawContact,然后参考它添加数据。要将新的 RawContact 链接到现有的,需要像 DisplayName 这样的 Datafiled。ContactsApp 会自动进行映射,但您必须信任。

这是解决方案,灵感来自 Android 示例中的 ContactsManager 应用程序

    private boolean createOrganizationFromContact(Contact contact, String companyName) {
        try {
        ContentResolver contentResolver = context.getContentResolver();
            ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
            operations.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, context.getResources().getString(R.string.accounttype))
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, context.getResources().getString(R.string.accountusername))
                .build());
        operations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Organization.DATA, companyName)
                .build());
        operations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contact.getDisplayName())
                .build());
        contentResolver.applyBatch(ContactsContract.AUTHORITY, operations);
        } catch (RemoteException e) {
            e.printStackTrace();
            return false;
        } catch (OperationApplicationException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
于 2013-06-20T09:17:38.530 回答