3

I am having some problem running my basic run time permission project.

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Initializing button
    buttonRequestPermission = (Button) findViewById(R.id.buttonRequestPermission);

    //Adding a click listener
    buttonRequestPermission.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //First checking if the app is already having the permission
            if (isReadStorageAllowed()) {
                //If permission is already having then showing the toast
                Toast.makeText(MainActivity.this, "You already have the permission", Toast.LENGTH_LONG).show();
                //Existing the method with return
                return;
            }

            //If the app has not the permission then asking for the permission
            requestStoragePermission();
        }
    });
}

//We are calling this method to check the permission status
private boolean isReadStorageAllowed() {
    //Getting the permission status
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

    //If permission is granted returning true
    if (result == PackageManager.PERMISSION_GRANTED)
        return true;

    //If permission is not granted returning false
    return false;
}
public void requestStoragePermission() {

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {

    }

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
    //Checking the request code of our request
    if (requestCode == STORAGE_PERMISSION_CODE) {

        //If permission is granted
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            //Displaying a toast
            Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
        } else {
            //Displaying another toast if permission is not granted
            Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
        }
    }
}

In this function, the program seems to crash in the requestPermissions() function.

When I test the app on a device, the app crashes and returns "Unfortunately, Package installation has stopped". Then gives me this logcat error:

Process: com.google.android.packageinstaller, PID: 17894
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to get length of null array
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477)
   at android.app.ActivityThread.access$900(ActivityThread.java:150)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5418)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
   at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:312)
   at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:362)
   at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:105)
   at android.app.Activity.performCreate(Activity.java:6285)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2477) 
   at android.app.ActivityThread.access$900(ActivityThread.java:150) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:5418) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

I have also placed this into the manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.angelos.permissiontest">
    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

    </application>

</manifest>

I am not really sure why this is happening. This should be a super simple project. Can somebody help me?

Angelo Charl
  • 347
  • 4
  • 15
  • Your error says that you'r runtime permisson not executed properly ! – Piyush Oct 04 '16 at 09:47
  • 2
    You should place the `` outside the ` tag.` Place it above it in your mqanifest – greenapps Oct 04 '16 at 10:07
  • Possible duplicate of [Package Installer crashes in Android M after requesting permission READ\_SMS](http://stackoverflow.com/questions/36171595/package-installer-crashes-in-android-m-after-requesting-permission-read-sms) – AxelH Oct 04 '16 at 10:12
  • That's it!!! It is now running and asking me the permissions. I placed the permission outside of the application. @greenapps – Angelo Charl Oct 04 '16 at 10:13
  • As @greenapps tell you, see this example https://developer.android.com/training/permissions/declaring.html for the manifest. – AxelH Oct 04 '16 at 10:18
  • Possible duplicate of [Getting Error while requesting READ\_CONTACTS permission Android M](http://stackoverflow.com/questions/37185927/getting-error-while-requesting-read-contacts-permission-android-m) – Mike M. Oct 04 '16 at 15:54

3 Answers3

3

This java.lang.NullPointerException is not because of your code. It is into com.android.packageinstaller.permission.ui.GrantPermissionsActivity.java. Below is code where you are getting java.lang.NullPointerException

309    private int computePermissionGrantState(PackageInfo callingPackageInfo,
310            String permission, int permissionPolicy) {
311        boolean permissionRequested = false;
312
313        for (int i = 0; i < callingPackageInfo.requestedPermissions.length; i++) {
314            .....
346    }

In you case, callingPackageInfo.requestedPermissions is null. So check if you declared requested permission into manifest correct.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
1

The answer here solved the problem as suggested by @greenapps and @Pankaj Kuma:

Putting the Permission tag outside of the application tag in the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.angelos.permissiontest">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

</application>

Reference : developer.android.com/training/permissions/declaring.html

Angelo Charl
  • 347
  • 4
  • 15
0
public void requestStoragePermission() {

if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
 // add the below line here
 ActivityCompat.requestPermissions(this, new String[]{


Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}

 // ActivityCompat.requestPermissions(this, new String[]    {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}

make below changes to to avoid crash

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

replace with below line

if(grantResults != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
Sush
  • 3,864
  • 2
  • 17
  • 35
  • It just avoids the null exception. – Reaz Murshed Oct 04 '16 at 09:40
  • I have tried this and it is still crashing with the same error. I have also tried commenting the if else statement in the onRequestPermissionsResult() function and same thing happens. – Angelo Charl Oct 04 '16 at 09:43
  • Putting requestPermissions() in the if statement will force it to skip. so the function will not be triggered. Thisi is hte link to the offictial documentation o this implementation. https://developer.android.com/training/permissions/requesting.html – Angelo Charl Oct 04 '16 at 09:53
  • @AngeloCharl please check ans – Sush Oct 04 '16 at 09:55