MapActivity 中只能有一个 MapView

发布于 2024-10-20 08:45:35 字数 1476 浏览 6 评论 0原文

我有一个显示地图页面的功能,这样我就可以让用户选择他们当前的位置。但是,如果您运行此函数两​​次,它会导致应用程序在 MapActivity 错误中使用单个 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: 错误/AndroidRuntime(28014):引起 通过:java.lang.IllegalStateException: 您只能拥有一个 MapActivity 中的 MapView

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

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

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): Caused
by: java.lang.IllegalStateException:
You are only allowed to have a single
MapView in a MapActivity

How can I delete the MapView and recreate it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

握住我的手 2024-10-27 08:45:35

我认为每个人都有点对,对我来说这似乎是 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);

这非常有效,让我有机会调用地图。感谢大家的回复。

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.

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.

I removed:

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

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

And replaced it with:

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.

固执像三岁 2024-10-27 08:45:35

因为您多次调用 setContentView(R.layout.set_map_center);
为什么这里还要重新设置contentView呢?为什么不把它放在onCreate中呢?

Because you call setContentView(R.layout.set_map_center); more than once.
Why do you need to set the contentView here again? Why not put it in onCreate?

人疚 2024-10-27 08:45:35

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

FragmentMap + ActionBar Tab

Here there is another solution that worked for me:

FragmentMap + ActionBar Tab

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文