Q:

Getting Error while requesting READ_CONTACTS permission Android M

i am trying to get READ_CONTACTS permission Android M and on calling: 
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);

app crash with following exception:
05-12 17:08:24.578: E/AndroidRuntime(2846): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to get length of null array
05-12 17:08:24.578: E/AndroidRuntime(2846):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at android.app.ActivityThread.-wrap11(ActivityThread.java)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at android.os.Looper.loop(Looper.java:148)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at android.app.ActivityThread.main(ActivityThread.java:5417)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at java.lang.reflect.Method.invoke(Native Method)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-12 17:08:24.578: E/AndroidRuntime(2846): Caused by: java.lang.NullPointerException: Attempt to get length of null array
05-12 17:08:24.578: E/AndroidRuntime(2846):     at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:293)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:343)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:100)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at android.app.Activity.performCreate(Activity.java:6251)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
05-12 17:08:24.578: E/AndroidRuntime(2846):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

I have also specified <uses-permission android:name="android.permission.READ_CONTACTS" /> in the Manifest file.

A:

Looking at the source for GrantPermissionsActivity, the only place in the computePermissionGrantState() method that you could get a NullPointerException from an Attempt to get length of null array is in this line:
for (int i = 0; i < callingPackageInfo.requestedPermissions.length; i++) {

Since you've stated that you have indeed listed the READ_CONTACTS permission in the manifest, it would follow that it's just in the wrong place, and is effectively not there. Ensuring that the <uses-permission> element is outside of the <application> tags solves the problem.

