Android application crashes at getContentResolver().query
Please find the attached code to retrieve the contact name for the incoming call. Here I hard coded the number as "123". I have added this contact in the emulator. But the application crashes at getContentResolver().query. Any help please.
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: com.android.internal.view.IInputMethodClient$Stub$Proxy@43b7f980
03-16 11:21:00.374: DEBUG/dalvikvm(65): GC freed开发者_StackOverflow 13348 objects / 660920 bytes in 285ms
Can you give the logs output from logcat. You need a context to call getContentResolver(). Are you calling this method in your activity class or somewhere else?
Edit: You need to add permissions to read the contacts from the phone's address book. Please follow this link: http://developer.android.com/reference/android/Manifest.permission.html#READ_CONTACTS
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
The solution is right in the error log you posted. Add permission READ_CONTACTS to your AndroidManifest You'll need a few other related permissions as well, look at the documentation for content to see which. -- which you need will depend on how you use the API.
精彩评论