"Attempt to get length of null array" 许可 运行 时间

"Attempt to get length of null array" on Permission run time

我在 运行我的基本 运行 时间许可项目中遇到了一些问题。

这是我的代码:

@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();
        }
    }
}

在这个函数中,程序似乎在requestPermissions()函数中崩溃了。

当我在设备上测试应用程序时,应用程序崩溃并且 returns "Unfortunately, Package installation has stopped"。然后给我这个 logcat 错误:

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.access0(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.access0(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) 

我也将其放入清单文件中:

<?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>

我不太确定为什么会这样。这应该是一个超级简单的项目。有人可以帮助我吗?

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)

替换为下一行

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

这个java.lang.NullPointerException不是因为你的代码。它变成了com.android.packageinstaller.permission.ui.GrantPermissionsActivity.java。以下是您获得 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    }

在你的例子中,callingPackageInfo.requestedPermissionsnull。因此,请检查您是否将请求的权限声明为正确的清单。

这里的答案解决了@greenapps 和@Pankaj Kuma 所建议的问题:

将 Permission 标签放在清单中的应用程序标签之外:

<?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>

参考:developer.android.com/training/permissions/declaring.html