您只允许有一个图形页面中MapActivity有一个、图形、您只、页面

2023-09-12 22:58:58 作者:屠宰↘一条街

我有一个函数,显示地图页面,所以我可以让用户选择自己的当前位置。但是,如果你在运行该函数两次崩溃的应用程序的单一图形页面中MapActivity错误(即开即设置再次查看)。

I have a function that shows a map page so I can get the user to choose their current location. But if you run this function twice it crashes the App with the Single MapView in a MapActivity error (i.e. opening that settings view again).

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。但是,如果你回到设置筛选出的地图,然后再次单击按钮,我得到这个错误:

So I have a button that shows this View and onClick="showMapSetterPage". But if you go back to the settings screen out of the map and click the button again I get this error:

03-06 20:55:54.091:   ERROR / AndroidRuntime(28014):因   按:java.lang.IllegalStateException:   你只能有一个单一的   图形页面在MapActivity

03-06 20:55:54.091: ERROR/AndroidRuntime(28014): Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity

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

How can I delete the MapView and recreate it?

推荐答案

我觉得都有些正确的,它接缝像API给我一个缺陷。我应该能够膨胀的视图,使用地图的话,如果我还记得的观点,​​然后才能删除或再次审核无误。

I think everyone was a little right, it seams like a flaw in the API to me. I should be able to inflate a view and use the map in it, if I recall the view then be able to delete it or review it again without error.

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

The easiest workaround was to remove the inflation or setContentView using the xml and going with a dynamic build of the map, then storing that in memory.

我删除:

// 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);

这个伟大的工程,给我的机会来调用地图。感谢您回应大家。

This works great and gives me chances to call the map. Thanks for responding everyone.