I want to be able to switch between a list fragment and a map fragment by the user by clicking the action bar button. Currently, I can exchange them among themselves just fine, but I came across a null pointer when trying to get the actual GoogleMap object from the fragment. When I try to move the camera, it does not move because the GoogleMap object is null and skips this code. I'm not sure if this is due to the fact that I never created the Snippet from xml, but used only the code? My code is as follows:
public class MapFragment extends SherlockMapFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = super.onCreateView(inflater, container, savedInstanceState);
return root;
}
}
@Override
public void swapFragments() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if (listFragment.isVisible()) {
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
transaction.replace(R.id.root, mapFragment);
} else {
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
transaction.replace(R.id.root, listFragment);
}
transaction.commit();
}
@Override
public void setupMapFragment() {
mapFragment = new MapFragment();
mMap = mapFragment.getMap();
if (mMap != null) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(BuzzbabaApplication.latitude,
BuzzbabaApplication.longitude), 14));
}
}
source
share