atz*_*jhu 3 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();
Run Code Online (Sandbox Code Playgroud)
CRO*_*OSP 11
你如何开始挑选联系活动?
试试这个.因为我没有设置意图类型而导致类似的问题.
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE_CONTACT);
Run Code Online (Sandbox Code Playgroud)
我希望这能帮到您.