当我使用此代码选择当前位置按钮时,我的谷歌地图崩溃了
if (v == locateMeButton) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
showGPSDisabledAlert();
} else {
List<String> providers = locationManager.getProviders(true);
Location location = null;
for (int i = providers.size() - 1; i >= 0; i--) {
location = locationManager.getLastKnownLocation(providers.get(i));
if (location != null){
break;
}
}
if (location != null) {
Float p1=Float.parseFloat(String.valueOf(location.getLatitude()));
Float p2=(Float.parseFloat(String.valueOf(location.getLongitude())));
int lat = (int) (p1 * 1E6);
int lon = (int) (p2 * 1E6);
GeoPoint geoPoint = new GeoPoint(lat, lon);
Map<String,String> mp=new HashMap<String, String>();
mp.put("latitude", String.valueOf(location.getLatitude()));
mp.put("longitude", String.valueOf(location.getLongitude()));
FlurryAgent.onEvent("Locations", mp);
OverlayItem overlayItem = new OverlayItem(geoPoint, null, null);
itemizedPosOverlay.clear();
itemizedPosOverlay.addOverlay(overlayItem);
mapOverlays.remove(itemizedPosOverlay);
mapOverlays.add(itemizedPosOverlay);
mapView.getController().animateTo(geoPoint);
} else {
Toast.makeText(InfoMapView.this, "Unable To Find Current Location",
Toast.LENGTH_LONG).show();
}
}
}
log cat如下:
: FATAL EXCEPTION: main
11-01 12:51:48.420: E/AndroidRuntime(24123): java.lang.ArrayIndexOutOfBoundsException
11-01 12:51:48.420: E/AndroidRuntime(24123): at java.util.ArrayList.get(ArrayList.java:313)
11-01 12:51:48.420: E/AndroidRuntime(24123): at com.countdown.tabs.InfoMapView.moreInfo(InfoMapView.java:153)
11-01 12:51:48.420: E/AndroidRuntime(24123): at com.countdown.map.MyItemizedOverlay.onInfoWindowTap(MyItemizedOverlay.java:55)
11-01 12:51:48.420: E/AndroidRuntime(24123): at com.countdown.map.InfoWindowItemizedOverlay$1.onClick(InfoWindowItemizedOverlay.java:78)
11-01 12:51:48.420: E/AndroidRuntime(24123): at android.view.View.performClick(View.java:2408)
11-01 12:51:48.420: E/AndroidRuntime(24123): at android.view.View$PerformClick.run(View.java:8816)
11-01 12:51:48.420: E/AndroidRuntime(24123): at android.os.Handler.handleCallback(Handler.java:587)
11-01 12:51:48.420: E/AndroidRuntime(24123): at android.os.Handler.dispatchMessage(Handler.java:92)
11-01 12:51:48.420: E/AndroidRuntime(24123): at android.os.Looper.loop(Looper.java:123)
11-01 12:51:48.420: E/AndroidRuntime(24123): at android.app.ActivityThread.main(ActivityThread.java:4633)
11-01 12:51:48.420: E/AndroidRuntime(24123): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 12:51:48.420: E/AndroidRuntime(24123): at java.lang.reflect.Method.invoke(Method.java:521)
11-01 12:51:48.420: E/AndroidRuntime(24123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-01 12:51:48.420: E/AndroidRuntime(24123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-01 12:51:48.420: E/AndroidRuntime(24123): at dalvik.system.NativeStart.main(Native Method)
11-01 12:51:48.430: W/ActivityManager(171): Force finishing activity net.timessquareball.app/com.countdown.TabBarControllerActivity
11-01 12:51:48.580: W/AudioFlinger(95): write blocked for 170 msecs, 1209 delayed writes, thread 0xbb50
答案 0 :(得分:2)
在某处出现错误:
List<String> providers = locationManager.getProviders(true);
或
for (int i = providers.size() - 1; i >= 0; i--) {
location = locationManager.getLastKnownLocation(providers.get(i));
因为你试图从ArrayList获取一个实际上不在索引中的对象。核实。
providers.size()
答案 1 :(得分:0)
for (int i = providers.size() - 1; i > 0; i--) {
需要注意的重要事项: i&gt; 0 强>
你不想处理0个案例,因为i = -1并且破坏了代码。
享受。