运行时错误:(53,55)错误:找不到符号方法checkSelfPermission(RuntimePermissionsActivity,String)

时间:2017-09-30 08:07:51

标签: android permissions

compileSdkVersion 23
    buildToolsVersion '25.0.0'
    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 23
    }

我的代码编译成功没有任何问题,但是当我运行它时,它会抛出下面的错误。

Error:(53, 45) error: cannot find symbol method checkSelfPermission(RuntimePermissionsActivity,String)

Error:(78, 27) error: cannot find symbol method requestPermissions(RuntimePermissionsActivity,String[],int)

Error:(120, 43) error: cannot find symbol method shouldShowRequestPermissionRationale(RuntimePermissionsActivity,String)

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
  

编译失败;有关详细信息,请参阅编译器错误输出。

1 个答案:

答案 0 :(得分:0)

在你的活动的onCreate()中,在if块中进行处理:(你收到错误是因为你可能要求在onCreate()中同时处理权限):

    askForPermissions();
    if(checkForPermission()){
        //Do your processing here
    }

功能是:

void askForPermissions(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
        }
    }
}

boolean checkForPermission(){
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}