【问题标题】:Android: Contact Picker Intent | Cannot instantiate the type UriAndroid:联系人选择器意图 |无法实例化类型 Uri
【发布时间】:2012-09-26 23:19:47
【问题描述】:

我正在尝试仅选择带有电话号码的联系人。我正在关注this code

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, new Uri("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

但不幸的是,它显示错误:Cannot instantiate the type Uri

实际上,我还有另一个工作代码可以完美运行,但在选择电子邮件联系人时会崩溃。我只需要电话号码。

Intent intentContact = new Intent(Intent.ACTION_PICK,
                                ContactsContract.Contacts.CONTENT_URI);
intentContact.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivityForResult(intentContact, PICK_CONTACT);

在onReceive(),这个方法被调用

public void getContactInfo(Intent intent) {

    ContentResolver cr = getContentResolver();
    cursor = cr.query(intent.getData(), null, null, null, null);

    while (cursor.moveToNext()) {
        String contactId = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID));
        if (Integer
                .parseInt(cursor.getString(cursor
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor phones = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = " + contactId, null, null);
            while (phones.moveToNext()) {
                phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
            phones.close();
        } else {
            snipp.showAlertDialog(getApplicationContext(), "No Number",
                    "Cannot read number", false);
        }

    }
    cursor.close();
}

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    这对我有用:

    private void pickContact() {
        Intent pickContactIntent = new Intent( Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI );
        pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
    }
    

    编辑:

    您的onActivityResult() 应如下所示:

    @Override
    public void onActivityResult( int requestCode, int resultCode, Intent intent ) {
    
        super.onActivityResult( requestCode, resultCode, intent );
        if ( requestCode == PICK_CONTACT_REQUEST ) {
    
            if ( resultCode == RESULT_OK ) {
                    Uri pickedPhoneNumber = intent.getData();
                    // handle the picked phone number in here.
                }
            }
        }
    }
    

    【讨论】:

    • 哪行代码导致了这个异常?您可以将整个堆栈粘贴到问题中吗?你在定义你的 onActivityResult() 吗?
    • 已修复。 :) 这是一个简单的修复..实际上是我的错误..感谢您的宝贵时间:)
    【解决方案2】:

    使用 Uri.parse() 代替。你不能直接实例化一个 Uri

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2013-01-16
      • 2019-03-31
      • 2019-07-08
      • 1970-01-01
      相关资源
      最近更新 更多