方法返回java.lang.IllegalArgumentException?

时间:2011-10-18 06:43:58

标签: android cursor uri android-contentprovider

我正在尝试使用其内容uri Contacts.CONTENT_URI

插入联系人

我只是声明了以下用于插入valuse的代码: -

    public void runContact()
{
    Uri u = Contacts.CONTENT_URI;

    ContentValues initialValues = new ContentValues();
    initialValues.put("data1", "1234567890");
    initialValues.put("data2", "Emergency");
    initialValues.put("data3", "Number");
    cr.query(u, null, null, null, null);
    Cursor cursor1 =  getContentResolver().query(u, null, null, null, null);
    if (cursor1.moveToFirst()) 
    {
        do {
            if((cursor1.getString(cursor1.getColumnIndex("address"))).equalsIgnoreCase("9953834074111"))
            {
                String data1 = cursor1.getString(cursor1.getColumnIndex("data1"));
                String data2 = cursor1.getString(cursor1.getColumnIndex("data2"));
                String data3 = cursor1.getString(cursor.getColumnIndex("data3"));
                Log.v("data1",data1);
                Log.v("data2",data2);
                Log.v("data3", data3);
            }
        } while (cursor1.moveToNext());
    }
}

每当我调用此方法时,它都会返回一个这样的异常 -

java.lang.IllegalArgumentException: URI: content://contacts, calling user: com.android.data, calling package:com.android.data

为什么会这样?我在那段代码中做错了什么?有人指导我吗?

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

private void addContacts(){

    try
    {
        Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        if (c != null) {
            while (c.moveToNext()) {
                if (c.getString(c.getColumnIndex(Phone.DISPLAY_NAME)).equalsIgnoreCase(officeText.getText().toString())) {
                    Log.d("ContactUSActivity", "Number Exist");
                    Toast.makeText(ContactUsActivity_2_0.this,getResources().getString(R.string.contact_already_exist),Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }

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

        Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
        builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,null);
        builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME,null);

        ContentProviderOperation contentProvider = builder.build();
        contentProviderArray.add(contentProvider);

        contentProviderArray.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0)
                .withValue(ContactsContract.Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.DISPLAY_NAME, officeText.getText().toString())
                .build());

        contentProviderArray.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0)
                .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_MOBILE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneText.getText().toString())
                .build());

        contentProviderArray.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,StructuredPostal.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY,addressText.getText().toString())             
                .build()); 

        /*contentProviderArray.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,StructuredPostal.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.,addressText.getText().toString())             
                .build()); */

        getContentResolver().applyBatch(ContactsContract.AUTHORITY, contentProviderArray);

        Toast.makeText(ContactUsActivity_2_0.this,getResources().getString(R.string.contact_added),Toast.LENGTH_SHORT).show();
    }
    catch (OperationApplicationException e) {
        Toast.makeText(ContactUsActivity_2_0.this,getResources().getString(R.string.contact_failed_to_added),Toast.LENGTH_SHORT).show();
    } catch (RemoteException e) {
        Toast.makeText(ContactUsActivity_2_0.this,getResources().getString(R.string.contact_failed_to_added),Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }       

}