无法解析 Manifest.permission. ACCESS_FINE_LOCATION

向清单文件添加权限时,下面的 xml 可以工作。

 <permission android:name="android.permission.ACCESS_FINE_LOCATION" />

但是,这个 xml 不能工作。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我该用哪个?如果是第一个,为什么不行?我该怎么补救?

另外,我得到了一个 Android 6.0运行时权限相关的异常:

java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.

当我尝试向 String 数组添加权限以检查权限时,Android Studio 告诉我它无法在下面的代码中解析 Manifest.permission:

new String[]{Manifest.permission.ACCESS_FINE_LOCATION}

为什么会这样? 我怎么才能修好它?

85910 次浏览

For the first part, you should be using <uses-permission> according the the Android Devlopers site. Try making sure you declare your permissions directly under the <manifest> tag, not in your <application> tag. It's hard to know what your problem is without seeing your entire manifest file. Check out the link I posted above for more info on how to declare permissions in your manifest.

In regards to your runtime permissions problem:

With uses-permissions Cannot resolve that..

new String[]{Manifest.permission.ACCESS_FINE_LOCATION}

Why?

Make sure you're using android.Manifest instead of my.app.package.Manifest. A lot of times Android Studio will default to the latter instead of the former.

So, your new line of code would look like:

new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION};

Edit: I reformatted my answer.

Edit 2: Be wary of importing android.Manifest. It can cause issues if you're also importing my.app.package.Manifest. Other than that import android.Manifest is another valid way to resolve this issue.

if you are working on dynamic permissions and any permission like ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION giving error "cannot resolve method PERMISSION_NAME" in this case write you code with permission name and then rebuild your project this will regenerate the manifest(Manifest.permission) file

When you press Alt+Enter on Manifest, Android Studio suggests a few options to import. You should choose the one import android.Manifest; instead of import <your.package.name>.Manifest;.

change this

Manifest.permission.ACCESS_FINE_LOCATION

into this

android.Manifest.permission.ACCESS_FINE_LOCATION

Try this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_SELECT_PICTURE);
return;
}
 if (Build.VERSION.SDK_INT >= 23) {


if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {


mapView.setMyLocationEnabled(true);
}
}
else
{
mapView.setMyLocationEnabled(true);


}

Try this before running, make sure you have permission to access..

try {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} catch (SecurityException e) {
dialogGPS(this.getContext()); // lets the user know there is a problem with the gps
}

Try this! Since ACCESS_FINE_LOCATION available in following package so Add:

import android.Manifest;

­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

I had a similar problem where used;

ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED

where it could not resolve symbol READ_CONTACTS.

However on using;

import android.Manifest;

It started to recognize READ_CONTACT

Change

new String[]{Manifest.permission.ACCESS_FINE_LOCATION}

To this

new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}

Your problem will be resolved.

new String[]{Manifest.permission.ACCESS_FINE_LOCATION}

change it to

android.manifest.permission.ACCESS_FINE_LOCATION

If you have already the uses.permissions setup correctly in your manifest file, as already mentioned by hnilsen, just replace your line:

 Manifest.permission.ACCESS_FINE_LOCATION

by this one:

 android.Manifest.permission.ACCESS_FINE_LOCATION

This can solve your problem.