私は自分のアプリから組み込みの連絡先を起動し、そこでモジュールから連絡先を選ぶ必要があるAndroidアプリケーション(Eclipse 3.5.2、Android 2.2を使用)を開発しています。私はそれのために次のコードを使用しています。私は組み込みの連絡先に立ち上げていいですが、連絡先を選択すると、プロジェクトの「onActivityResult」に戻ります。「アプリケーションが予期せず停止し、強制終了しました」という結果がクラッシュします。私はただ1つのテストコンタクトが追加されたエミュレータでテストしています。この行でクラッシュが発生しています"カーソルカーソル= managedQuery(intent.getData()、null、null、null、null);"Android:連絡先を選択した後にクラッシュする
ここで間違っていることを教えてもらえますか?
int PICK_CONTACT;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SetupView();
}
private void SetupView()
{
final Button selectContactBtn = (Button) findViewById(R.id.selectcontactBtn);
//selectContactBtn.setText("Test Button Text");
selectContactBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode==PICK_CONTACT && intent != null)
{
getContactInfo(intent);
}
}
protected void getContactInfo(Intent intent)
{
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);// CRASH HERE
cursor.moveToNext();
String contactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(this, "Contact Name: "+name, Toast.LENGTH_LONG).show();
}
LOGCAT:
08-13 18:15:45.075: INFO/ActivityManager(58): Displayed activity com.jonsofttech.smsacontact/.SMSaContactActivity: 6579 ms (total 6579 ms)
08-13 18:16:00.225: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.PICK typ=vnd.android.cursor.dir/contact cmp=com.android.contacts/.ContactsListActivity }
08-13 18:16:00.756: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44feba98 com.jonsofttech.smsacontact/.SMSaContactActivity}
08-13 18:16:00.925: INFO/ContactsListActivity(159): Called with action: android.intent.action.PICK
08-13 18:16:02.215: INFO/ActivityManager(58): Displayed activity com.android.contacts/.ContactsListActivity: 1461 ms (total 1461 ms)
08-13 18:16:22.695: WARN/ActivityManager(58): Launch timeout has expired, giving up wake lock!
08-13 18:16:22.775: WARN/ActivityManager(58): Activity idle timeout for HistoryRecord{44feba98 com.jonsofttech.smsacontact/.SMSaContactActivity}
08-13 18:16:35.015: ERROR/DatabaseUtils(159): Writing exception to parcel
08-13 18:16:35.015: ERROR/DatabaseUtils(159): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts/lookup/0r1-4A4E2C2E2C402C4E444A/1 from pid=956, uid=10036 requires android.permission.READ_CONTACTS
08-13 18:16:35.015: ERROR/DatabaseUtils(159): at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:271)
08-13 18:16:35.015: ERROR/DatabaseUtils(159): at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:149)
08-13 18:16:35.015: ERROR/DatabaseUtils(159): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111)
08-13 18:16:35.015: ERROR/DatabaseUtils(159): at android.os.Binder.execTransact(Binder.java:288)
08-13 18:16:35.015: ERROR/DatabaseUtils(159): at dalvik.system.NativeStart.run(Native Method)
08-13 18:16:51.146: WARN/dalvikvm(956): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): FATAL EXCEPTION: main
08-13 18:16:54.216: ERROR/AndroidRuntime(956): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-4A4E2C2E2C402C4E444A/1 (has extras) }} to activity {com.jonsofttech.smsacontact/com.jonsofttech.smsacontact.SMSaContactActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts/lookup/0r1-4A4E2C2E2C402C4E444A/1 from pid=956, uid=10036 requires android.permission.READ_CONTACTS
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.os.Looper.loop(Looper.java:123)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at java.lang.reflect.Method.invoke(Method.java:521)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at dalvik.system.NativeStart.main(Native Method)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts/lookup/0r1-4A4E2C2E2C402C4E444A/1 from pid=956, uid=10036 requires android.permission.READ_CONTACTS
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.os.Parcel.readException(Parcel.java:1247)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.content.ContentResolver.query(ContentResolver.java:245)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.app.Activity.managedQuery(Activity.java:1520)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at com.jonsofttech.smsacontact.SMSaContactActivity.getContactInfo(SMSaContactActivity.java:47)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at com.jonsofttech.smsacontact.SMSaContactActivity.onActivityResult(SMSaContactActivity.java:42)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
08-13 18:16:54.216: ERROR/AndroidRuntime(956): ... 11 more
08-13 18:16:54.475: WARN/ActivityManager(58): Force finishing activity com.jonsofttech.smsacontact/.SMSaContactActivity
08-13 18:16:55.136: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44feba98 com.jonsofttech.smsacontact/.SMSaContactActivity}
08-13 18:17:07.556: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{44feba98 com.jonsofttech.smsacontact/.SMSaContactActivity}
例外が表示されている間、あなたはlogcatの出力を提供することができればそれは大いに役立つだろう。スタックトレースにはエラー – Janusz
の説明があるはずです。LogCatを追加しました。 – Getsy