2012-02-05 61 views
0

我嘗試使用Google maps API開發Android地圖應用程序。java.lang.SecurityException:提供者gps需要ACCESS_FINE_LOCATION權限

使用Google API測試幾個emulatet Android設備(2.1; 2.3.3; 4.0.3)。

的問題是,logcat中拋出這個我:

02-05 04:39:57.519: E/AndroidRuntime(588): FATAL EXCEPTION: main 
02-05 04:39:57.519: E/AndroidRuntime(588): java.lang.RuntimeException: Unable to start activity ComponentInfo{map.test.activity/map.test.activity.MapTestActivity}: java.lang.SecurityException: Provider gps requires ACCESS_FINE_LOCATION permission 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.os.Looper.loop(Looper.java:130) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-05 04:39:57.519: E/AndroidRuntime(588): at java.lang.reflect.Method.invokeNative(Native Method) 
02-05 04:39:57.519: E/AndroidRuntime(588): at java.lang.reflect.Method.invoke(Method.java:507) 
02-05 04:39:57.519: E/AndroidRuntime(588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-05 04:39:57.519: E/AndroidRuntime(588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-05 04:39:57.519: E/AndroidRuntime(588): at dalvik.system.NativeStart.main(Native Method) 
02-05 04:39:57.519: E/AndroidRuntime(588): Caused by: java.lang.SecurityException: Provider gps requires ACCESS_FINE_LOCATION permission 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.os.Parcel.readException(Parcel.java:1322) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.os.Parcel.readException(Parcel.java:1276) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:582) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446) 
02-05 04:39:57.519: E/AndroidRuntime(588): at map.test.activity.MapTestActivity.onCreate(MapTestActivity.java:29) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-05 04:39:57.519: E/AndroidRuntime(588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
02-05 04:39:57.519: E/AndroidRuntime(588): ... 11 more 

下面是代碼:

package map.test.activity; 

import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapController; 
import com.google.android.maps.MapView; 

public class MapTestActivity extends MapActivity { 

    private MapController mapController; 
    private MapView mapView; 
    private LocationManager locationManager; 

    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     setContentView(R.layout.main); // bind the layout to the activity 

     mapView = (MapView) findViewById(R.id.mapview); 
     mapView.setBuiltInZoomControls(true); 
     mapController = mapView.getController(); 
     mapController.setZoom(10); // Zoom 1 is world view 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 
         0, new GeoUpdateHandler()); 
    } //public void 

    @Override 
    protected boolean isRouteDisplayed() { 
     return false; 
    }// protected boolean 

    public class GeoUpdateHandler implements LocationListener { 

      public void onLocationChanged(Location location) { 
        int lat = (int) (location.getLatitude() * 1E6); 
        int lng = (int) (location.getLongitude() * 1E6); 
        GeoPoint point = new GeoPoint(lat, lng); 
        mapController.animateTo(point); 
      } 

      public void onProviderDisabled(String provider) { 
       // TODO Auto-generated method stub 

      } 

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

      } 

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

      } 
    } 

} //public class MapTestActivity 

且相應的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="map.test.activity" android:versionCode="1" 
    android:versionName="1.0"> 

    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
    <uses-permission android:name="android.permission.ACCES_COARSE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.ACCES_FINE_LOCATION"></uses-permission> 

    <uses-sdk android:minSdkVersion="10" /> 

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> 
     <activity android:name=".MapTestActivity" android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <uses-library android:required="true" android:name="com.google.android.maps" /> 
    </application> 
</manifest> 

我已經嘗試轉變的位置的權限並將編譯器設置爲JAVA 1.6(並將@Override添加到最後三個公共空隙中)。 這可能是非常明顯的東西,但我剛開始用Java開發。

回答

-1

非常愚蠢的錯誤,你已經完成。你不能從OnCreate()方法上面的行刪除"@Override「。加入該行並嘗試再次運行代碼。

+0

不幸的是我不斷收到同樣的錯誤。 – Teonee 2012-02-05 06:56:40

+0

@Override只是一個註釋,建議使用它來指定重寫,但對相關代碼的操作沒有直接影響... – 2012-02-05 07:07:59

+0

謝謝@Rahulgarg ..... – Lucifer 2012-02-05 11:36:51

7

拼錯

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

應該

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

不會修復錯誤。不過謝謝。 – Teonee 2012-02-05 08:07:49

+1

你可以給出logcat錯誤消息,解決此問題後.... – 2012-02-05 09:39:51

+1

罰款位置權限應該解決此問題。你確定它仍然產生相同的異常嗎? – tomwhipple 2012-04-05 19:21:43

1

我得到了完全相同的錯誤消息,因爲你做過,但在我的情況下,原來是因爲我剛剛使用

permission 

代替

uses-permission 

希望這會一個下午救人調試;-)

7

確保使用的許可元素不是在應用程序中的元素。我不小心將它粘貼在那裏,Android Studio不會告訴你這是一個問題。一旦我將其移出,它就解決了這個問題。

例子:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.android.someapp.app" > 

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

    <application ...> 

... 
相關問題