Cannot resolve method 'checkSelfPermission' in 'ContextCompat'
时间: 2023-12-28 13:04:41 浏览: 63
This error message usually occurs when you are trying to use the `checkSelfPermission()` method from the `ContextCompat` class, but the method cannot be resolved. The reason for this is that the `checkSelfPermission()` method was introduced in API level 23 (Android Marshmallow) and if your app is targeting an API level lower than 23, then this method won't be available.
To fix this error, you can either:
1. Update your app's targetSdkVersion to 23 or higher in your app's build.gradle file. This will make the `checkSelfPermission()` method available.
2. Use the `ActivityCompat` class instead of `ContextCompat`, which has a similar `checkSelfPermission()` method that is available in all API levels.
For example, instead of:
```
ContextCompat.checkSelfPermission(context, permission)
```
You can use:
```
ActivityCompat.checkSelfPermission(activity, permission)
```
Make sure to import the `ActivityCompat` class at the top of your file:
```
import androidx.core.app.ActivityCompat;
```
阅读全文