2016-11-14 63 views
0

我已经创建了一个简单的android工作室项目,并带有Google地图活动。当我尝试拨打mMap.setMyLocationEnabled(true);时,我的问题开始 - >它需要检查清单中的许可,但我在加载Manifest.permission.ACCESS_FINE_LOCATION上的权限时遇到了一些问题。无法加载它,它是红色的。从清单文件加载权限

的方法:

@Override 
public void onMapReady(GoogleMap googleMap) 
{ 
    mMap = googleMap; 

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
      == PackageManager.PERMISSION_GRANTED) { 
     mMap.setMyLocationEnabled(true); 
    } else { 
     Log.d("mes:", "error"); 
    } 
} 

我的清单文件:

<?xml version="1.0" encoding="utf-8"?> 

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


<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_maps_key"/> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

    <activity 
     android:name="sei0055.placebook.MapsActivity" 
     android:label="@string/title_activity_maps"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"/> 

      <category android:name="android.intent.category.LAUNCHER"/> 
     </intent-filter> 
    </activity> 
</application> 

+2

从Android 6.0开始,必须在运行时请求和授予权限:https://developer.android.com/training/permissions/requesting.html - 没有“从清单中检查”以获取危险权限。您正在检查但未请求。 –

+0

“不能加载它,它是红色的” - 如果你的意思是说'ACCESS_FINE_LOCATION'是红色的,或者'Manifest.permission.ACCESS_FINE_LOCATION'是红色的,你需要添加一个适当的Java'import'语句[for android.Manifest .permission'](https://developer.android.com/reference/android/Manifest.permission.html),就像任何其他Java类一样。 – CommonsWare

+0

我的意思是ACCESS_FINE_LOCATION是红色的 – JerryX

回答

0

尝试

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
     == PackageManager.PERMISSION_GRANTED) {}