android ContextCompat.checkSelfPermission() 未找到

android ContextCompat.checkSelfPermission() not found

我正在尝试使运行时权限向后兼容旧设备,但使用支持库我无法在 ContextCompat class 中找到 checkSelfPermission() 静态调用,但其记录在案 here

这是我的项目gradle设置:

defaultConfig {
    applicationId "org.myprogram.cool"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 39
    versionName "3.0"
}

这里是依赖项:

compile 'com.google.android.gms:play-services:+'
compile 'com.squareup:otto:1.3.5'
compile 'com.android.support:appcompat-v7:22.1.1'

知道我错过了什么吗?

checkSelfPermission() 直到 API 23 才引入本机 Context。因此您至少需要使用支持库的版本 23.0.0。

你应该改变这个

compile 'com.android.support:appcompat-v7:22.1.1'

至此

compile 'com.android.support:appcompat-v7:23.0.1'

如果您已从 eclipse 迁移,添加以下行将不起作用

compile 'com.android.support:appcompat-v7:23.0.1'

迁移时,将appcompatv4添加为外部依赖库。 确保你已经从 libs 文件夹中删除了 appcompatv4 库,然后它应该开始工作

  1. 确保 class exampleActivity class 从 AppCompatActivity.

  2. 延伸
  3. 确保你的 minSdkVersion 在 build.gradle 文件中是 23。

  4. 进行此更改后同步您的 gradle。

应该可以。