我有一个 MainActivity,它有一个带有两个选项卡的 ActionBar,第一个选项卡是显示传感器数据值,第二个选项卡是我想显示地图。但是我在第二个选项卡的片段中实现地图时遇到问题
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 如下所示 public class MyTabsListener implements ActionBar.TabListener { private Fragment fragment;
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();
}
}
我在“mapView.onCreate(savedInstanceState);”行的 FragmentMap 类中设置了一个断点 savedInstanceState 为空,程序将在行后崩溃。
有人可以帮忙吗。提前致谢。