【问题标题】:Call a mapfragment in an AppCompatAcivity在 AppCompatAcivity 中调用 mapfragment
【发布时间】:2016-04-09 12:02:31
【问题描述】:

我正在尝试在片段寻呼机的活动中使用谷歌地图,因此地图位于寻呼机后面并且仍然能够与之交互,但无论我做什么,我都会遇到空异常。

我尝试过这样使用 SupportMapFragment:

SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.Map));
        mapFragment.getMapAsync(this);

但 getChildFragmentManager 无法解析,因为我猜是AppCompatActivity。 还有另一种方法吗?可能吗?谢谢。

【问题讨论】:

  • 改成getSupportFragmentManager()

标签: java android android-fragments google-maps-api-3 supportmapfragment


【解决方案1】:

尝试动态添加它。

        MapFragment mMapFragment = MapFragment.newInstance();
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.llMapContainer, mMapFragment, "map");
        fragmentTransaction.commit();

否则你可以像这样得到地图片段

        mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
        if (mapFragment != null) {
            mapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap map) {
                   loadMap(map);
                }
           });
        }

【讨论】:

  • 谢谢!我动态地做了它并且工作得非常好:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-24
  • 1970-01-01
  • 2017-06-29
相关资源
最近更新 更多