我嘗試使用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開發。
不幸的是我不斷收到同樣的錯誤。 – Teonee 2012-02-05 06:56:40
@Override只是一個註釋,建議使用它來指定重寫,但對相關代碼的操作沒有直接影響... – 2012-02-05 07:07:59
謝謝@Rahulgarg ..... – Lucifer 2012-02-05 11:36:51