2015-06-26 54 views
0

我已經做了一個簡單的應用程序,在用戶的當前位置顯示一個標記,並且我使用了清單中的所有權限。下面是代碼:未授予Android ACCESS_FINE_LOCATION權限或其他內容?

public class LocationActivity extends FragmentActivity implements LocationListener { 
SupportMapFragment map; 
GoogleMap googleMap; 
LocationManager loc; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_location); 
    map=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    onMapReady(googleMap); 
    //onLocationChanged(location); 
} 
public void onMapReady(GoogleMap googleMap){ 
    try { 
     googleMap = map.getMap(); 
     if (googleMap != null) { 
      googleMap.setMyLocationEnabled(true); 
      loc = (LocationManager) getSystemService(LOCATION_SERVICE); 
      Criteria crit = new Criteria(); 
      String best = loc.getBestProvider(crit,true); 
      Location location = loc.getLastKnownLocation(best); 
      LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude()); 
      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 15)); 
      googleMap.addMarker(new MarkerOptions().title("You're here").position(myLocation)); 
     } else { 
      Toast.makeText(this, "Fragment has a problem", Toast.LENGTH_SHORT).show(); 
     } 
    } 
    catch (Exception e){ 
     Toast.makeText(this,"we can't do it",Toast.LENGTH_SHORT).show(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_location, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 
} 

@Override 
public void onLocationChanged(Location location) { 
//something will go on here 
}} 

但它顯示了一個運行時錯誤:

ACCESS_FINE_LOCATION is required for PRIORITY_HIGH_LOCATION_REQUEST

什麼是這裏的可能的原因?雖然代碼編譯好,如果我刪除此行 -

googleMap.setMyLocationEnabled(true); 

但位置不accurate.` 這裏是清單文件

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

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <meta-data 
    android:name="com.google.android.geo.API_KEY" 
    android:value="API_Key"/> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
    <android:uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> 
    <uses-permission android:name="com.example.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 
    <activity 
     android:name=".LockScreenActivity" 
     android:label="@string/title_activity_lock_screen" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".LocationActivity" 
     android:label="@string/title_activity_location" > 
    </activity> 
</application> 

+0

讓我們知道清單snnippet .. plz添加清單片段來解決錯誤 –

+0

添加清單。 PLZ檢查:) –

+0

花花公子將所有權限 外'應用程序'標記 –

回答

1

兩個問題你的包名:

  1. <uses-permission><permission>,並<uses-feature>元素需要向外移動的所有<application>。他們應該是根的<manifest>元素的直接子女。

  2. 您不要在元素名稱上使用android:前綴,就像您在READ_PHONE_STATEREAD_EXTERNAL_STORAGE中使用的那樣。無論如何,您並不需要READ_EXTERNAL_STORAGE,因爲您需要WRITE_EXTERNAL_STORAGE

+0

它對我很有用,但現在它顯示了來自地圖的藍色圓圈而不是標記。 –

1

把以下所有代碼片段放在application標籤

<android:uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> 
    <uses-permission android:name="com.example.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 

而且也是在一次添加添加權限

<permission android:name="yourpackagename.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> 
<uses-permission android:name="yourpackagename.permission.MAPS_RECEIVE" />