遇到 checkSelfPermission 时 Context Compat 出错

Context Compat having error when met with checkSelfPermission

我一直在尝试对 Android 执行简单的权限检查。

if (ContextCompat.checkSelfPermission(permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                        alertboxGranted();
                }

我一直在尝试支持 4x Android(基本上 <23 API)

(另外,仅供参考,我已经在清单和程序开始时导入了所需的组件)

但是,当我添加上面的 ContextCompat 时,出现了无法将 checkSelfPermission 应用于 java.lang.Strings

的错误

但是 ContextCompat 基本上不是用来支持 checkSelfPermission 的吗?我不确定为什么会出现该错误。任何人都可以建议为什么会出现该错误吗?

谢谢!

However, when I add the above ContextCompat, it comes up with error that the checkSelfPermission cannot be applied to java.lang.Strings

正确。您缺少 the Context first parameter to the method.

  private boolean canGetLocation() {
    return(ContextCompat.checkSelfPermission(this,
      Manifest.permission.ACCESS_FINE_LOCATION)==
      PackageManager.PERMISSION_GRANTED);
  }