1

我有一个显示地图页面的功能,因此我可以让用户选择他们当前的位置。但是,如果您两次运行此功能,它会在 MapActivity 错误中使用 Single MapView 使应用程序崩溃(即再次打开该设置视图)。

public void showMapSetterPage(View v) {
    Log.i(DEBUG_TAG, "Settings screen, set map center launched");

    // Set which view object we fired off from
    set_pv(v);

    // Show Map Settings Screen
    setContentView(R.layout.set_map_center);

    // Initiate the center point map
    if (mapView == null) {
        mapView = (MapView) findViewById(R.id.mapview);
    }

    mapView.setLongClickable(true);
    mapView.setStreetView(true);
    mapView.setBuiltInZoomControls(false);
    mapView.setSatellite(false);

    mapController = mapView.getController();
    mapController.setZoom(18);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = lm
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    int lat = (int) (location.getLatitude() * 1E6);
    int lng = (int) (location.getLongitude() * 1E6);

    Log.i(DEBUG_TAG, "The LAT and LONG is: " + lat + " == " + lng);

    point = new GeoPoint(lat, lng);

    // mapController.setCenter(point);
    mapController.animateTo(point);
}

所以我有一个按钮显示这个视图和 onClick="showMapSetterPage"。但是,如果您返回地图外的设置屏幕并再次单击按钮,我会收到此错误:

03-06 20:55:54.091: ERROR/AndroidRuntime(28014): Caused by: java.lang.IllegalStateException: 你只能在 MapActivity 中拥有一个 MapView

如何删除 MapView 并重新创建它?

4

3 回答 3

3

我认为每个人都说得对,对我来说这似乎是 API 中的一个缺陷。我应该能够膨胀视图并在其中使用地图,如果我记得该视图,则可以删除它或再次查看它而不会出错。

最简单的解决方法是使用 xml 删除通货膨胀或 setContentView 并使用地图的动态构建,然后将其存储在内存中。

我删除了:

// Show Map Settings Screen
setContentView(R.layout.set_map_center);

// Initiate the center point map
if (mapView == null) {
    mapView = (MapView) findViewById(R.id.mapview);
}

并将其替换为:

if (mapView == null) {
   // public MapView mapView = null; // Public defined Variable
   mapView = new MapView(this, this.getString(R.string.APIMapKey));
}

setContentView(mapView);

这很好用,让我有机会调用地图。感谢大家回复。

于 2011-03-07T08:07:39.887 回答
0

因为你setContentView(R.layout.set_map_center);不止一次打电话。为什么这里需要再次设置contentView?为什么不放进去onCreate

于 2011-03-07T07:50:12.263 回答
0

这里有另一个对我有用的解决方案:

FragmentMap + ActionBar 选项卡

于 2012-01-13T11:01:21.327 回答