如何解决检查权限中获得的nullpointer异常

时间:2016-08-19 00:06:50

标签: android android-6.0-marshmallow

目前我正在使用Marshmallow SDK并检查用户是否已为我所做的每个位置调用授予了权限,但每次请求位置时我都会得到空指针的这个例外。这就是我使用检查权限的方式

 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            }

            if ( Build.VERSION.SDK_INT >= 23 &&
                    ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
                    ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                //return 0 ;
            }

我的日志猫在

之下
08-19 00:57:16.537 10992-10992/com.ant.henry.gatesng W/System.err:     java.lang.NullPointerException
08-19 00:57:16.539 10992-10992/com.ant.henry.gatesng W/System.err:     at android.content.ContextWrapper.checkPermission(ContextWrapper.java:545)
08-19 00:57:16.539 10992-10992/com.ant.henry.gatesng W/System.err:     at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:432)
08-19 00:57:16.539 10992-10992/com.ant.henry.gatesng W/System.err:     at com.ant.henry.gatesng.GPSService.getLocation(GPSService.java:76)
08-19 00:57:16.539 10992-10992/com.ant.henry.gatesng W/System.err:     at com.ant.henry.gatesng.CapeActivity.nearBy(CapeActivity.java:190)
08-19 00:57:16.539 10992-10992/com.ant.henry.gatesng W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 00:57:16.539 10992-10992/com.ant.henry.gatesng W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
08-19 00:57:16.539 10992-10992/com.ant.henry.gatesng W/System.err:     at android.view.View$1.onClick(View.java:3851)
08-19 00:57:16.540 10992-10992/com.ant.henry.gatesng W/System.err:     at android.view.View.performClick(View.java:4471)
08-19 00:57:16.540 10992-10992/com.ant.henry.gatesng W/System.err:     at android.view.View$PerformClick.run(View.java:18789)
08-19 00:57:16.540 10992-10992/com.ant.henry.gatesng W/System.err:     at android.os.Handler.handleCallback(Handler.java:808)
08-19 00:57:16.540 10992-10992/com.ant.henry.gatesng W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:103)
08-19 00:57:16.540 10992-10992/com.ant.henry.gatesng W/System.err:     at android.os.Looper.loop(Looper.java:193)
08-19 00:57:16.540 10992-10992/com.ant.henry.gatesng W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5323)
08-19 00:57:16.540 10992-10992/com.ant.henry.gatesng W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 00:57:16.540 10992-10992/com.ant.henry.gatesng W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
08-19 00:57:16.540 10992-10992/com.ant.henry.gatesng W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
08-19 00:57:16.540 10992-10992/com.ant.henry.gatesng W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
08-19 00:57:16.541 10992-10992/com.ant.henry.gatesng W/System.err:     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您可以更新有关GPSService的源代码吗?

如果您不能,请检查如下所示将this更改为GPSService.this

if ( Build.VERSION.SDK_INT >= 23 ) {
    if (ContextCompat.checkSelfPermission(GPSService.this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
        ContextCompat.checkSelfPermission(GPSService.this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(GPSService.this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {
            Log.i(TAG, "Waiting user's response...");

        } else {
            ActivityCompat.requestPermissions(GPSService.this,
                    new String[]{
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION
                    },
                    100); // 100 will be used in "onRequestPermissionsResult" about "requestCode".
        }
    }
}

然后,您必须覆盖自定义服务中的onRequestPermissionsResult

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 100:
            if (grantResults.length > 0
                    || grantResults[0] == PackageManager.PERMISSION_GRANTED
                    || grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "Permissions are granted. Application is available now.");

            } else {
                Log.e(TAG, "Permissions are denied. Application is disabled.");
            }
    }
}