【发布时间】:2015-04-28 09:23:03
【问题描述】:
我希望将 Android 地图 v2 插入片段,但我的限制是我不能使用 .xml 文件。当我尝试实例化 MapFragment 时出现问题。代码如下:
public class LocationFragment extends Fragment{
private static View view;
private static GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
view = (RelativeLayout) inflater.inflate(R.layout.location_fragment, container, false);
setUpMapIfNeeded(); // For setting up the MapFragment
MapFragment mf = new MapFragment();
return view;
}
/***** Sets up the map if it is possible to do so *****/
public static void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
FragmentManager manager = MapActivity.fragmentManager;
MapFragment smf = (MapFragment) manager.findFragmentById(R.id.location_map);
mMap = smf.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
setUpMap();
}
}
有人可以帮帮我吗?
【问题讨论】:
标签: android google-maps fragment mapfragment