Java 无法解析方法 checkSelfPermission

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34238246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:27:29  来源:igfitidea点击:

Cannot resolve method checkSelfPermission

javaandroidandroid-studiopermissions

提问by Phil

I'm trying to make my app ready for Android 6 and now I'm stuck to the point where you need to request and check permissions.

我正在尝试让我的应用程序为 Android 6 做好准备,但现在我遇到了需要请求和检查权限的问题。

I tried the following from the docs:

我从文档中尝试了以下内容:

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR);

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR);

The problem is that Android Studio says Cannot resolve method 'checkSelfPermission'.

问题是 Android Studio 说Cannot resolve method 'checkSelfPermission'.

I already included the appcompat and support lib. ContextCompatis known to AS but the method itself isn't known. I don't know what am I doing wrong - in another project I can write this method and it gets recognized.

我已经包含了 appcompat 和支持库。ContextCompatAS已知,但方法本身未知。我不知道我做错了什么 - 在另一个项目中我可以编写这个方法并且它得到了认可。

TargetAPI is 23.

TargetAPI 为 23。

Does anyone know a solution?

有谁知道解决方案?

采纳答案by Phil

Oh my godness - what a stupid mistake.

哦,天哪 - 多么愚蠢的错误。

AS imported the supportlib as a jar and this jar was from like 2014. I just replaced the jarimport with the real dependency and know it is working.

AS 将 supportlib 作为 jar 导入,这个 jar 来自 2014 年。我只是用真正的依赖项替换了 jarimport 并且知道它正在工作。

Thanks for your help guys!

谢谢你们的帮助!

回答by Manikanta

Here is how you need to call in various scenarios,

以下是您在各种情况下需要调用的方式,

In case of activity:

在活动的情况下:

 ContextCompat.checkSelfPermission(MyActivity.this,
        Manifest.permission.WRITE_CALENDAR);

In case of fragment:

在片段的情况下:

 ContextCompat.checkSelfPermission(getActivity(),
        Manifest.permission.WRITE_CALENDAR);

In case of any utility class use context:

在任何实用程序类使用上下文的情况下:

 ContextCompat.checkSelfPermission(context,
        Manifest.permission.WRITE_CALENDAR);

Comment below for further information

在下方评论以获取更多信息

回答by Ali Akram

For Fragmentuse getActivity().checkSelfPermission

Fragment使用getActivity().checkSelfPermission

For Activityuse this..checkSelfPermissionor simply checkSelfPermission

对于Activity使用this..checkSelfPermission或简单地checkSelfPermission

回答by Fahad Jadun

@SuppressLint("NewApi")

I simply used this on top of my page and it works for me...

我只是在我的页面顶部使用了它,它对我有用......

回答by gavin

As silly as it maybe, it could be in the wrong place. I had the same problem. The bolded part is where I had originally put the code. The italic part is where it should have gone

尽管它可能很愚蠢,但它可能出现在错误的地方。我有同样的问题。粗体部分是我最初放置代码的地方。斜体部分是它应该去的地方

locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        Log.i("-----------", location.toString());
    }
    **if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.ACCESS_FINE_LOCATION) != 
            PackageManager.PERMISSION_GRANTED) {'some code'}**
    }; 'End of LocationListener method
    *if (ContextCompat.checkSelfPermission(this, 
        Manifest.permission.ACCESS_FINE_LOCATION) != 
        PackageManager.PERMISSION_GRANTED) { 'some code'}*

回答by P1x

I had the same problem. In my case I added a library that was using an old appcompat version, then the compiler could not find the right appcompat.

我有同样的问题。就我而言,我添加了一个使用旧 appcompat 版本的库,然后编译器找不到正确的 appcompat。

To fix the problem I added the option {transitive = false} while importing the culprit library, and this fixed the problem.

为了解决这个问题,我在导入罪魁祸首库时添加了选项 {transitive = false},这解决了问题。

Now I have:

我现在有:

api ('org.library.using.old.appcompat:1.0.1') {transitive = false}

api ('org.library.using.old.appcompat:1.0.1') {transitive = false}

回答by codingjeremy

Trying to use checkSelfPermission()in a Fragmentwith Kotlin and wondering how to get around Contextbeing null?

尝试使用checkSelfPermission()Fragment与科特林,不知道怎么去解决Context是空?

Have a look at the sample below, and remember, before the Fragmentis attached to the Activity, the Contextwill be null.

看看下面的示例,记住,在Fragment附加到之前ActivityContext将为空。

private fun fineLocationPermissionApproved(): Boolean {

    val context = context ?: return false

    return PackageManager.PERMISSION_GRANTED == checkSelfPermission(
        context,
        Manifest.permission.ACCESS_FINE_LOCATION
    )
}