试图获取联系信息无效的列数据1

时间:2014-12-31 23:16:43

标签: java android exception android-contentprovider

我正在尝试获取有关所选联系人的信息,但不幸的是我开始异常Caused by: java.lang.IllegalArgumentException: Invalid column data1 我复制了Android手册中的代码。这是我的代码。我从结果方法的活动中的片段做到这一点。数据不为null,它在查询方法中抛出异常。

case REQUEST_CODE_CONTACT:
                        // Get the URI that points to the selected contact
                        Uri contactUri = data.getData();
                        // We only need the NUMBER column, because there will be only one row in the result
                        String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};

                        // Perform the query on the contact to get the NUMBER column
                        // We don't need a selection or sort order (there's only one result for the given URI)
                        // CAUTION: The query() method should be called from a separate thread to avoid blocking
                        // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
                        // Consider using CursorLoader to perform the query.
                        Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(contactUri, projection, null, null, null);
                        cursor.moveToFirst();

                        // Retrieve the phone number from the NUMBER column
                        int columnNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                        int columnName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                        String number = cursor.getString(columnNumber);
                        String name = cursor.getString(columnName);

                        Toast.makeText(getActivity().getApplicationContext(),"You have selected "+name + " " + number,Toast.LENGTH_LONG).show();

3 个答案:

答案 0 :(得分:10)

您如何开始选择联系活动?
试试这个。因为我没有设置意图类型而导致类似的问题。

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        startActivityForResult(intent, REQUEST_CODE_CONTACT);

我希望这会对你有所帮助。

答案 1 :(得分:0)

试试这段代码:

点击按钮启动 contactPickerIntent

tvaddressbook.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent contactPickerIntent = new    Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
                    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);

                }
            });

然后 OnActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (resultCode == RESULT_OK) {
        switch (requestCode) 
        {
        case CONTACT_PICKER_RESULT:
            Cursor cursor = null;
            String email = "", name = "";
            try {
                Uri result = data.getData();
                Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());

                // get the contact id from the Uri
                String id = result.getLastPathSegment();

                // query for everything email
                cursor = getContentResolver().query(Email.CONTENT_URI,  null, Email.CONTACT_ID + "=?", new String[] { id }, null);

                int nameId = cursor.getColumnIndex(Contacts.DISPLAY_NAME);

                int emailIdx = cursor.getColumnIndex(Email.DATA);

                // let's just get the first email
                if (cursor.moveToFirst()) {
                    email = cursor.getString(emailIdx);
                    name = cursor.getString(nameId);
                    Log.v(DEBUG_TAG, "Got email: " + email);
                } else {
                    Log.w(DEBUG_TAG, "No results");
                }
            } catch (Exception e) {
                Log.e(DEBUG_TAG, "Failed to get email data", e);
            } finally {
                if (cursor != null) {
                    cursor.close();
                }

                edsharewith.setText(email);

                if (email.length() == 0 && name.length() == 0) 
                {
                    Toast.makeText(this, "No Email for Selected Contact",Toast.LENGTH_LONG).show();
                }
            }
            break;
        }

    } else {
        Log.w(DEBUG_TAG, "Warning: activity result not ok");
    }
}

字符串名称和电子邮件将构成特定联系人的姓名和电子邮件。

答案 2 :(得分:0)

使用

private fun getContactByNumber() {
                val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
                intent.setDataAndType(ContactsContract.Contacts.CONTENT_URI, ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE)
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                startActivityForResult(intent, RQ_CONTACTS)
    }