請查找附加代碼以檢索傳入呼叫的聯繫人姓名。在這裏,我把這個數字硬編碼爲「123」。我在模擬器中添加了這個聯繫人。但是應用程序在getContentResolver()。查詢中崩潰。請任何幫助。Android應用程序在getContentResolver()中崩潰查詢
String number = "123";
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup.NUMBER };
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor c = getContentResolver().query(contactUri, projection, null,
null, null);
// if the query returns 1 or more results
// return the first result
if (c.moveToFirst()) {
String name = c.getString(c
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
Log.i("Map", "Contact Name " + name);
}
03-16 11:12:09.304: INFO/ActivityManager(65): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=ContactName.com/.ContactName }
03-16 11:12:09.544: INFO/ActivityManager(65): Start proc ContactName.com for activity ContactName.com/.ContactName: pid=406 uid=10024 gids={}
03-16 11:12:10.284: DEBUG/ddm-heap(406): Got feature list request
03-16 11:12:11.103: ERROR/DatabaseUtils(145): Writing exception to parcel
03-16 11:12:11.103: ERROR/DatabaseUtils(145): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/phone_lookup/9895318420 from pid=406, uid=10024 requires android.permission.READ_CONTACTS
03-16 11:12:11.103: ERROR/DatabaseUtils(145): at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:240)
03-16 11:12:11.103: ERROR/DatabaseUtils(145): at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:116)
03-16 11:12:11.103: ERROR/DatabaseUtils(145): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:98)
03-16 11:12:11.103: ERROR/DatabaseUtils(145): at android.os.Binder.execTransact(Binder.java:287)
03-16 11:12:11.103: ERROR/DatabaseUtils(145): at dalvik.system.NativeStart.run(Native Method)
03-16 11:12:11.194: DEBUG/AndroidRuntime(406): Shutting down VM
03-16 11:12:11.204: WARN/dalvikvm(406): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-16 11:12:11.214: ERROR/AndroidRuntime(406): Uncaught handler: thread main exiting due to uncaught exception
03-16 11:12:11.284: ERROR/AndroidRuntime(406): java.lang.RuntimeException: Unable to start activity ComponentInfo{ContactName.com/ContactName.com.ContactName}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/phone_lookup/9895318420 from pid=406, uid=10024 requires android.permission.READ_CONTACTS
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.os.Looper.loop(Looper.java:123)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at java.lang.reflect.Method.invoke(Method.java:521)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at dalvik.system.NativeStart.main(Native Method)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/phone_lookup/9895318420 from pid=406, uid=10024 requires android.permission.READ_CONTACTS
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.os.Parcel.readException(Parcel.java:1218)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.content.ContentProviderProxy.bulkQuery(ContentProviderNative.java:326)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.content.ContentProviderProxy.query(ContentProviderNative.java:345)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.content.ContentResolver.query(ContentResolver.java:202)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at ContactName.com.ContactName.onCreate(ContactName.java:31)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-16 11:12:11.284: ERROR/AndroidRuntime(406): ... 11 more
03-16 11:12:11.344: INFO/Process(65): Sending signal. PID: 406 SIG: 3
03-16 11:12:11.354: INFO/dalvikvm(406): threadid=7: reacting to signal 3
03-16 11:12:11.354: ERROR/dalvikvm(406): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
03-16 11:12:19.364: WARN/ActivityManager(65): Launch timeout has expired, giving up wake lock!
03-16 11:12:20.454: WARN/ActivityManager(65): Activity idle timeout for HistoryRecord{43ea9568 ContactName.com/.ContactName}
03-16 11:17:11.394: INFO/Process(406): Sending signal. PID: 406 SIG: 9
03-16 11:17:11.454: INFO/ActivityManager(65): Process ContactName.com (pid 406) has died.
03-16 11:17:11.684: ERROR/gralloc(65): [unregister] handle 0x388370 still locked (state=40000001)
03-16 11:17:11.724: INFO/UsageStats(65): Unexpected resume of com.android.launcher while already resumed in ContactName.com
03-16 11:17:11.914: WARN/InputManagerService(65): Window already focused, ignoring focus gain of: [email protected]
03-16 11:21:00.374: DEBUG/dalvikvm(65): GC freed 13348 objects/660920 bytes in 285ms
你可以給logcat輸出日誌嗎? – Ankit 2011-03-16 11:15:06
日誌已添加。請檢查 – indira 2011-03-16 11:25:53
好像你沒有添加讀取聯繫人的權限。檢查此鏈接:http://developer.android.com/reference/android/Manifest.permission.html#READ_CONTACTS – Ankit 2011-03-16 11:29:01