方法返回java.lang.IllegalArgumentException?

发布于 2024-12-10 05:18:34 字数 1286 浏览 0 评论 0原文

我正在尝试插入联系人及其内容 uri Contacts.CONTENT_URI

我只是声明以下代码来插入值: -

    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, 调用 user: com.android.data, 调用 package:com.android.data

为什么会发生这种情况?我在该代码中做错了什么?有人指导我吗?

I'm trying to insert the contacts with their content uri Contacts.CONTENT_URI

I just declaring the following code for inserting the 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());
    }
}

Whenever i called this method, it returns an exception like this -

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

Why this happened? What i'm doing wrong in that code? Anyone guide me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2024-12-17 05:18:34

尝试像下面的代码:

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();
    }       

}

Try like below code:

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();
    }       

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文