【发布时间】:2014-04-19 20:15:28
【问题描述】:
我可以建立一个谷歌地图的实例并查看地图,但我不能 getMap(),它总是为空:
mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.container, mMapFragment);
fragmentTransaction.commit();
if (mMap == null) {
mMap = mMapFragment.getMap();
// always null, why?
if (mMap != null) {
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
}
【问题讨论】:
-
替换 Fragment 是异步的,因此该 Fragment 可能尚未放置在您的布局中,因此使 getMap 为您提供 null。在此 mapFragment 事务结束后,您应该使用 getMap 方法替换/添加您的 mapFragment 和地图请求
标签: android google-maps