在android中运行时检查权限时获取NullPointerException

时间:2017-12-28 17:19:48

标签: android nullpointerexception android-permissions

以下是 MainActivity 代码。来自 MainActivity permissionCheck()方法的 onCreate()方法调用MultiplePermission 类。

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(Build.VERSION.SDK_INT >= 23){
            MultiplePermission multiplePermission = new MultiplePermission();
            if(multiplePermission.permissionCheck()){
                Toast.makeText(this, "All permission granted", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

MultiplePermission 类代码如下。 此类的 permissionCheck()方法在运行时检查android权限。如果拒绝任何权限,则关闭整个应用程序。

public class MultiplePermission extends AppCompatActivity {
    private int PERMISSION_ALL = 1;
    private String[] PERMISSIONS = {
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.PROCESS_OUTGOING_CALLS
    };

    public boolean permissionCheck() {
        if (!hasPermissions(this, PERMISSIONS)) {

            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }
        return true;
    }

    private boolean hasPermissions(Context context,String[] permissions) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission((Activity)context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        for(int result : grantResults){
            if(result != PackageManager.PERMISSION_GRANTED)
                System.exit(0);
        }
        return;
    }
}

这是错误信息。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference
                                                                                            at android.content.ContextWrapper.checkPermission(ContextWrapper.java:669)
                                                                                            at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:453)
                                                                                            at testapp.android.vogella.com.permissiontest.MultiplePermission.hasPermissions(MultiplePermission.java:40)
                                                                                            at testapp.android.vogella.com.permissiontest.MultiplePermission.permissionCheck(MultiplePermission.java:29)
                                                                                            at testapp.android.vogella.com.permissiontest.MainActivity.onCreate(MainActivity.java:16)
                                                                                            at android.app.Activity.performCreate(Activity.java:6664)
                                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                                            at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                            at android.os.Looper.loop(Looper.java:154) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

1 个答案:

答案 0 :(得分:1)

您永远不应该使用Activity启动new Object(),它不会获得context引用。

MultiplePermission不应该是Activity,并且在您需要上下文的方法中,通过参数获取它:

public boolean permissionCheck(Activity activity) {
    if (!hasPermissions(this, PERMISSIONS)) {

        ActivityCompat.requestPermissions(activity, PERMISSIONS, PERMISSION_ALL);
    }
    return true;
}