我有一个MainActivity,它有一个带有两个标签的ActionBar,第一个标签用于显示传感器数据值,第二个标签用于显示Map。 但是我在第二个标签上的片段中实现地图有问题
MainActivity如下所示:
public class MainActivity extends Activity
{
private static final String DATA_FRAGMENT_BAR = "Data";
private static final String MAP_FRAGMENT_BAR = "Map";
private DataCollection mDataCollection;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Config.mContext = this;
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME| ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab dataTab = actionBar.newTab().setText(DATA_FRAGMENT_BAR);
ActionBar.Tab mapTab = actionBar.newTab().setText(MAP_FRAGMENT_BAR);
Fragment dataFragment = new FragmentData();
Fragment mapFragment = new FragmentMap();
dataTab.setTabListener(new MyTabsListener(dataFragment));
mapTab.setTabListener(new MyTabsListener(mapFragment));
actionBar.addTab(dataTab);
actionBar.addTab(mapTab);
mDataCollection = DataCollection.getInstance();
}
....
}
TabsListener如下所示 公共类MyTabsListener实现ActionBar.TabListener { 私有片段片段;
public MyTabsListener(Fragment fragment)
{
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(fragment);
}
}
地图片段类如下所示:
public class FragmentMap extends Fragment
{
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_map, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
//map.getUiSettings().setMyLocationButtonEnabled(false);
//map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions().position(new LatLng(50.167003,19.383262)));
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try
{
MapsInitializer.initialize(this.getActivity());
}
catch (GooglePlayServicesNotAvailableException e)
{
e.printStackTrace();
}
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
return v;
}
@Override
public void onResume()
{
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy()
{
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory()
{
super.onLowMemory();
mapView.onLowMemory();
}
}
我在FragmentMap类的“mapView.onCreate(savedInstanceState);”行设置了一个断点。 savedInstanceState为null,程序将在行后崩溃。
有人可以提供帮助。提前谢谢。
答案 0 :(得分:0)
Google发布了Map API版本2.这为您提供了MapFragment和SupportMapFragment。这允许您在不扩展MapActivity的情况下添加Map。
答案 1 :(得分:0)
文档中并不是很清楚,但应在 MapView.onCreate()
中调用Fragment.onActivityCreated()
方法,而不是Fragment.onCreate()
此外,MapView.getMap()
可以返回null,不要忘记检查。
您应该在super.onResume()
mapView.onResume()