片段中的 MapView(蜂窝) [英] MapView in a Fragment (Honeycomb)

查看:26
本文介绍了片段中的 MapView(蜂窝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

既然最终的 SDK 与 google apis 一起发布了 - 使用 MapView 创建 Fragment 的最佳方法是什么?MapView 需要 MapActivity 才能正常工作.

让管理 Fragment 的 Activity 从 MapActivity 继承(糟糕的解决方案,因为它违背了 Fragment 是自包含的想法)并且使用常规的基于 xml 的布局不起作用.我在 MapActivity.setupMapView() 中得到 NullPointerException:

<前>E/AndroidRuntime(597):引起:java.lang.NullPointerExceptionE/AndroidRuntime(597): 在 com.google.android.maps.MapActivity.setupMapView(MapActivity.java:400)E/AndroidRuntime(597):在 com.google.android.maps.MapView.(MapView.java:289)E/AndroidRuntime(597):在 com.google.android.maps.MapView.(MapView.java:264)E/AndroidRuntime(597):在 com.google.android.maps.MapView.(MapView.java:247)

我的第二个想法是以编程方式创建 MapView 并将关联的活动(通过 getActivity())作为上下文传递给 MapView 构造函数.不起作用:

<前>E/AndroidRuntime(834):由:java.lang.IllegalArgumentException:MapViews 只能在 MapActivity 的实例中创建.E/AndroidRuntime(834):在 com.google.android.maps.MapView.(MapView.java:291)E/AndroidRuntime(834):在 com.google.android.maps.MapView.(MapView.java:235)E/AndroidRuntime(834): 在 de.foo.FinderMapFragment.onCreateView(FinderMapFragment.java:225)E/AndroidRuntime(834): 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:708)E/AndroidRuntime(834): 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)E/AndroidRuntime(834): 在 android.app.FragmentManagerImpl.addFragment(FragmentManager.java:978)E/AndroidRuntime(834): 在 android.app.Activity.onCreateView(Activity.java:4090)E/AndroidRuntime(834): 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)

真的应该有像 MapFragment 这样的东西来处理 MapView 需要的后台线程,我猜......那么目前这样做的最佳实践是什么?

来自德国的感谢和问候,瓦伦丁

解决方案

截至 2012 年 12 月 3 日,Google 发布了 Google Maps Android API v2.现在你可以忘记这些问题了.https://developers.google.com/maps/documentation/android/>

使用新 API 的示例 - https://developers.google.com/maps/documentation/android/start#add_a_map

此 API 至少适用于 Android API 8,因此请使用它;)

所以现在您可以简单地使用com.google.android.gms.maps.MapFragment"片段类.它将在您的活动中显示地图.上面链接中的布局示例:

now that the final SDK is out with google apis - what is the best way to create a Fragment with a MapView? MapView needs a MapActivity to work right.

Having the Activity managing the Fragments inherit from MapActivity (bad solution because it goes against the idea that Fragments are self contained) and use a regular xml based layout does not work. I get a NullPointerException in MapActivity.setupMapView():

E/AndroidRuntime(  597): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  597):    at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:400)
E/AndroidRuntime(  597):    at com.google.android.maps.MapView.(MapView.java:289)
E/AndroidRuntime(  597):    at com.google.android.maps.MapView.(MapView.java:264)
E/AndroidRuntime(  597):    at com.google.android.maps.MapView.(MapView.java:247)

My second idea was to create the MapView programmatically and pass the associated activity (via getActivity()) as Context to the MapView constructor. Does not work:

E/AndroidRuntime(  834): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
E/AndroidRuntime(  834):    at com.google.android.maps.MapView.(MapView.java:291)
E/AndroidRuntime(  834):    at com.google.android.maps.MapView.(MapView.java:235)
E/AndroidRuntime(  834):    at de.foo.FinderMapFragment.onCreateView(FinderMapFragment.java:225)
E/AndroidRuntime(  834):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:708)
E/AndroidRuntime(  834):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
E/AndroidRuntime(  834):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:978)
E/AndroidRuntime(  834):    at android.app.Activity.onCreateView(Activity.java:4090)
E/AndroidRuntime(  834):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)

Really there should be something like MapFragment that takes care of the background threads MapView needs I guess... So what is the current best practice to do this?

Thanks and regards from Germany, Valentin

解决方案

As of 03.12.2012 Google released Google Maps Android API v2. Now you can forget about these problems. https://developers.google.com/maps/documentation/android/

Example using new API - https://developers.google.com/maps/documentation/android/start#add_a_map

This API will work for at least Android API 8, so use it ;).

So now you can simply use "com.google.android.gms.maps.MapFragment" fragment class. It will display the map in your Activity. Layout example from the link above:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment"/>

这篇关于片段中的 MapView(蜂窝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆