Java Android运行时权限GPS:尝试调用虚拟方法';android.content.pm.PackageManager android.content.Context.getPackageManager()';

Java Android运行时权限GPS:尝试调用虚拟方法';android.content.pm.PackageManager android.content.Context.getPackageManager()';,java,android,cordova,android-permissions,runtime-permissions,Java,Android,Cordova,Android Permissions,Runtime Permissions,我正在开发一个基于cordova的Android项目,我有一个使用GPS定位的类,我想实现新的权限运行时 我遵循了GitHub的官方文档和google项目,但我发现以下错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.pm.PackageManager android.content.Context.getPackageManager()” 在这一行: if(ActivityCompat.shouldShowRe

我正在开发一个基于cordova的Android项目,我有一个使用GPS定位的类,我想实现新的权限运行时

我遵循了GitHub的官方文档和google项目,但我发现以下错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.pm.PackageManager android.content.Context.getPackageManager()”

在这一行:

if(ActivityCompat.shouldShowRequestPermissionRegulation(GpsTrackingActivity.this、Manifest.permission.ACCESS\u FINE\u LOCATION))

代码如下:

private boolean checkPermission(){
    int result = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
    if (result == PackageManager.PERMISSION_GRANTED){

        return true;

    } else {

        return false;

    }
}

private void requestPermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(GpsTrackingActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)){

        android.widget.Toast.makeText(getApplicationContext(),"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.", android.widget.Toast.LENGTH_LONG).show();

    } else {

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

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {


                Log.i(TAG_DBG, "Permission Granted, Now you can access location data.");
            } else {


                Log.i(TAG_DBG, "Please request permission");
            }
            break;
    }
}

public void startTravel(String travelId, CarProperties car) {

    Log.i(TAG_DBG, "L'utilisateur � appuy� sur Start");
    //askForPermission(Manifest.permission.ACCESS_FINE_LOCATION,LOCATION);

    if (checkPermission()) {


        Log.i(TAG_DBG," Permission already granted");
    } else {
        Log.i(TAG_DBG, "Please request permission");

    }
    if (!checkPermission()) {

        requestPermission();

    } else {

        Log.i(TAG_DBG," Permission already granted");
    }

    location = locationManager.getLastKnownLocation(provider);
    //fb:bb#105: minTime to 1/2s instead of 1s to increase the number of fixes calculating effiMiles
    locationManager.requestLocationUpdates(provider, 500, 10, this);
    ...
    ...
    ...
}

我做错了什么

为什么
startTravel
LocationManager
上进行需要权限的调用?权限请求是异步的,因此在收到
onRequestPermissionsResult
回调之前,您不知道请求是否已被授予。我也有同样的问题。你有没有想过这个问题?