使用ACTION_EDIT启动活动并传递LOOKUP_KEY会导致NumberFormatException

时间:2011-10-17 17:55:30

标签: android

我正在尝试从我的应用程序中添加android中的联系人。为此,我获取所有数据并通过意图传递它。

而且,我想要做的是插入时,它会打开Add Contact屏幕,这样用户就可以编辑它,所以我正在尝试使用ACTION_EDIT。

public void addNewContactToDevice() 
    {   
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactInsertIndex = ops.size();
        ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
            .withValue(RawContacts.ACCOUNT_TYPE, null)
            .withValue(RawContacts.ACCOUNT_NAME, null)
            .build());
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
            .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
            .withValue(StructuredName.GIVEN_NAME, person.getName())
            .withValue(StructuredName.FAMILY_NAME, person.getLastName())
            .build());  
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
            .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
            .withValue(Phone.NUMBER, person.getPhone())
            .withValue(Phone.TYPE, Phone.TYPE_MOBILE)                
            .build());

        try 
        {
            ((Panel)activity).getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            Cursor contactsCursor = activity.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                    new String[] {ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID}, 
                    ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?" , 
                    new String[] { PhoneNumberUtils.formatNumber(individualSelected.getPhone()) }, null);

            contactsCursor.moveToFirst();

            String androidContactId = contactsCursor.getString(contactsCursor.getColumnIndex(LOOKUP_KEY));

            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + androidContactId));
            ((Panel)activity).startActivityForResult(intent, 2);
        } 
        catch (RemoteException e) 
        {
            e.printStackTrace();
        } 
        catch (OperationApplicationException e) 
        {
            e.printStackTrace();
        }   

    }

但问题是,传递ACTION_EDIT会为LOOKUP_KEY返回一个NumberFormatException(类似于这个0r17-3E483A46442C32324838)。当它试图打开Add Contact屏幕时,它无法解析LOOKUP_KEY。

我应该做什么?

感谢。


更新:这是logcat

10-18 08:54:59.003: ERROR/AndroidRuntime(7527): Caused by: java.lang.NumberFormatException: 0r17-3E483A46442C32324838
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at java.lang.Long.parse(Long.java:364)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at java.lang.Long.parseLong(Long.java:354)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at java.lang.Long.parseLong(Long.java:320)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at android.content.ContentUris.parseId(ContentUris.java:41)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at com.android.contacts.ui.EditContactActivity$QueryEntitiesTask.doInBackground(EditContactActivity.java:497)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at com.android.contacts.ui.EditContactActivity$QueryEntitiesTask.doInBackground(EditContactActivity.java:471)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at com.android.contacts.util.WeakAsyncTask.doInBackground(WeakAsyncTask.java:45)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-18 08:54:59.003: ERROR/AndroidRuntime(7527):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

1 个答案:

答案 0 :(得分:2)

我找到了一个解决方案,而不是将其传递给setData方法:

intent.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + androidContactId));

我应该通过这个:

Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup_key);
Uri res = ContactsContract.Contacts.lookupContact(activity.getContentResolver(), lookupUri);

在setData()方法中,我传递变量res,即URI类型。


question给了我很多帮助!