我正在使用Google https://developer.android.com/guide/components/intents-common.html中的示例 可以使用以下代码启动联系人浏览器。
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, PHONE_ACTIVITY);
}
我选择了一个联系人,活动退出并调用我的onActivityResult()
然而,当我使用示例代码从Intent获取结果时,它会在到达查询的最后一行时抛出非法列异常data1()
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (PHONE_ACTIVITY): {
if (resultCode == Activity.RESULT_OK) {
Uri contactUri = data.getData();
String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContentResolver().query(contactUri, projection,
null, null, null);
}}}}
我不知道出了什么问题。数据在Uri中,但我得到以下堆栈转储。
11-04 19:24:01.711: E/AndroidRuntime(5790): FATAL EXCEPTION: main
11-04 19:24:01.711: E/AndroidRuntime(5790): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/2942i5ae4d5758914f9df.3247i232/225 flg=0x1 }} to activity {}: java.lang.IllegalArgumentException: Invalid column data1
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.app.ActivityThread.deliverResults(ActivityThread.java:3322)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3365)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.app.ActivityThread.access$1200(ActivityThread.java:141)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1315)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.os.Looper.loop(Looper.java:137)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.app.ActivityThread.main(ActivityThread.java:5059)
11-04 19:24:01.711: E/AndroidRuntime(5790): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 19:24:01.711: E/AndroidRuntime(5790): at java.lang.reflect.Method.invoke(Method.java:511)
11-04 19:24:01.711: E/AndroidRuntime(5790): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
11-04 19:24:01.711: E/AndroidRuntime(5790): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
11-04 19:24:01.711: E/AndroidRuntime(5790): at dalvik.system.NativeStart.main(Native Method)
11-04 19:24:01.711: E/AndroidRuntime(5790): Caused by: java.lang.IllegalArgumentException: Invalid column data1
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.content.ContentProviderProxy.query(ContentProviderNative.java:374)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.content.ContentResolver.query(ContentResolver.java:370)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.content.ContentResolver.query(ContentResolver.java:313)
11-04 19:24:01.711: E/AndroidRuntime(5790): at
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.app.Activity.dispatchActivityResult(Activity.java:5242)
11-04 19:24:01.711: E/AndroidRuntime(5790): at android.app.ActivityThread.deliverResults(ActivityThread.java:3318)
11-04 19:24:01.711: E/AndroidRuntime(5790): ... 11 more
答案 0 :(得分:0)
好的,如果有人感兴趣的话,谷歌的例子有一个错误。一般的方法是正确的,你用代码调用联系人,但这再次分解为联系人界面有点复杂和记录不足。我将投影设置为null并像这样解析出数据。
ContentResolver contentResolver = getContentResolver();
Cursor cursor = getContentResolver().query(contactUri,
null, null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
String contact_id = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
int numberIndex = cursor
.getColumnIndex(CommonDataKinds.Phone.NUMBER);
int hasPhoneNumber = Integer
.parseInt(cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor phoneCursor = contentResolver
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[] { contact_id }, null);
boolean found = false;
while (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneType = phoneCursor.getInt(phoneCursor
.getColumnIndex(Phone.TYPE));
if (phoneType == Phone.TYPE_MOBILE) {
phoneNumber = phoneNumber.replaceAll(
"[^\\d]", "");
if (phoneNumber.startsWith("1")) {
phoneNumber = phoneNumber.substring(1,
phoneNumber.length());
}}}}