我正在尝试通过按按钮来获取手机联系人。但我正在接受例外处理。
06-20 09:23:29.039: I/ApplicationPackageManager(21730): cscCountry is not German : INU
06-20 09:23:46.929: D/AndroidRuntime(21730): Shutting down VM
06-20 09:23:46.929: W/dalvikvm(21730): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-20 09:23:46.960: E/AndroidRuntime(21730): FATAL EXCEPTION: main
06-20 09:23:46.960: E/AndroidRuntime(21730): java.lang.IllegalArgumentException: URI: content://contacts/, calling user: android.uid.system:1000, calling package is one of: [com.sec.android.app.factorytest, com.sec.android.app.popupuireceiver, com.android.settings.mt, com.android.systemui, com.wssnps, com.sec.android.app.lcdtest, com.samsung.AlertRecipients, com.android.providers.settings, com.android.providers.security, com.samsung.InputEventApp, com.sec.android.providers.drm, android, com.android.Preconfig, com.android.bluetoothtest, com.android.server.vpn, com.google.android.backup, com.sec.android.app.wlantest, com.sec.android.app.personalization, com.sec.app.RilErrorNotifier, com.android.settings, com.wipereceiver, com.android.providers.subscribedfeeds, com.sec.android.app.selftestmode, com.sec.android.app.controlpanel, com.sec.android.app.servicemodeapp]
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.os.Parcel.readException(Parcel.java:1326)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.os.Parcel.readException(Parcel.java:1276)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.app.ActivityManagerProxy.getProviderMimeType(ActivityManagerNative.java:2843)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.content.ContentResolver.getType(ContentResolver.java:215)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.content.Intent.resolveType(Intent.java:3268)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.content.Intent.resolveTypeIfNeeded(Intent.java:3290)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.app.Activity.startActivityForResult(Activity.java:2827)
06-20 09:23:46.960: E/AndroidRuntime(21730): at com.example.contactpicker.ContentPickerTester$1.onClick(ContentPickerTester.java:29)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.view.View.performClick(View.java:2485)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.view.View$PerformClick.run(View.java:9080)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.os.Handler.handleCallback(Handler.java:587)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.os.Looper.loop(Looper.java:130)
06-20 09:23:46.960: E/AndroidRuntime(21730): at android.app.ActivityThread.main(ActivityThread.java:3687)
06-20 09:23:46.960: E/AndroidRuntime(21730): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 09:23:46.960: E/AndroidRuntime(21730): at java.lang.reflect.Method.invoke(Method.java:507)
06-20 09:23:46.960: E/AndroidRuntime(21730): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-20 09:23:46.960: E/AndroidRuntime(21730): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-20 09:23:46.960: E/AndroidRuntime(21730): at dalvik.system.NativeStart.main(Native Method)
我在以下代码
的后续行中遇到异常和我的源代码 ContentPickerTester.java
startActivityForResult(intent,PICK_CONTACT);
package com.example.contactpicker;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ContentPickerTester extends Activity {
public static final int PICK_CONTACT = 1;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.contentpickertester);
Button button = (Button)findViewById(R.id.pick_contact_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View _view) {
Intent intent = new Intent(Intent.ACTION_PICK,
Uri.parse("content://contacts/"));
startActivityForResult(intent, PICK_CONTACT);
}
});
}
@Override
public void onActivityResult(int reqCode, int resCode, Intent data) {
super.onActivityResult(reqCode, resCode, data);
switch(reqCode) {
case (PICK_CONTACT) : {
if (resCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
c.moveToFirst();
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
tv.setText(name);
}
break;
}
}
}
}
我在以下代码
的后续行中遇到异常答案 0 :(得分:2)
尝试以下代码:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
// ...
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
tv.setText(name);
}
}
}
答案 1 :(得分:1)
您可以通过这种方式传递intent
。
Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 0);
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (i == reqCode && resultCode == Activity.RESULT_OK) {
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex("data1"));
name=phones.getString(phones.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
System.out.println("NAME:"+name+"\n NUMBER:"+cNumber);
}
}
}
}
工作很棒,我总是习惯这个。
答案 2 :(得分:0)
将以下行包含在
下的AndroidManifest.xml中<uses-permission android:name="android.permission.READ_CONTACTS" />