2016-11-14 46 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) {}