Android 在带有选项卡的onCreateView()中使用Google地图片段
我的Android 在带有选项卡的onCreateView()中使用Google地图片段,android,android-maps-v2,Android,Android Maps V2,我的FragmentActivity扩展了ActionBar.TabListener,有两个选项卡 在我的第一个选项卡中,我试图显示GoogleMap 如何在我的onCreateView()方法中执行此操作?这是我的代码: public class MapFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundl
FragmentActivity
扩展了ActionBar.TabListener
,有两个选项卡
在我的第一个选项卡中,我试图显示GoogleMap
如何在我的onCreateView()方法中执行此操作?这是我的代码:
public class MapFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
this.ctx = container.getContext();
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
return rootView;
}
}
这段代码在getMap()
上给我发了一个错误:
类型MapFragment的方法getMap()未定义
这是我的XML布局片段_map.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
我敢打赌,你一定会升级你的谷歌游戏服务库
公开最终GoogleMap getMap()
此方法已弃用
改为使用getMapAsync(OnMapReadyCallback)。回调方法为您提供一个保证为非null且随时可用的GoogleMap实例
现在需要使用而不是getMap()
检查此链接将您的类名MapFragment
更改为Else您可以编辑您的问题并发布您的MapFragment
的XML吗?也是来自android.support.v4
的片段吗?是的,我的片段
来自android.support.v4
谢谢。我正在尝试使用getMapAsync。。如果有帮助,我会发短信给你!我正在关注Google文档[(我的类扩展了Fragment而不是FragmentActivity,因为它是我活动的一个选项卡),但它在getMapAsync
上给了我一个错误。我该怎么办?你说的错误?我忘记了我的水晶球。你应该向我描述^getMapAsync(上下文)方法的错误类型MapFragment未定义
我需要更多信息,请参阅您问题的注释。