I'm trying to add Google Maps to a fragment using the v4 support lib, but I have some trouble using getMap() to reference it.
I'm adding the fragment like so (following the example at http://developer.android.com/training/basics/fragments/fragment-ui.html#AddAtRuntime) :
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
MapFragmentClass mapFragmentClass= new MapFragmentClass ();
mapFragmentClass.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, mapFragmentClass).commit();
}
}
}
And in the mapFragmentClass I'm trying to get the reference like this:
public class MapFragmentClass extends Fragment {
public static GoogleMap mMap;
private static SupportMapFragment mapFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
SupportMapFragment mapFragment = SupportMapFragment.newInstance();
mMap = mapFragment.getMap();
return inflater.inflate(R.layout.mapfragment, container, false);
}
or like this
@Override
public void onResume(){
super.onResume();
mMap = mapFragment.getMap();
Log.i("TestMap", "setCamera");
}
Both options come back with a null.. So basically I don't have a reference to the map when the Fragment is active. I even also tried getting a reference 10 seconds after the map was loaded, but alas..
Why won't it return a GoogleMap? The var mapFragment is always filled, from the moment I instantiate it in onCreateView, so if that's filled, I should be able to get a GoogleMap type back right then & there, correct?
I've tried loading in a GoogleMap a normal Activity, using a Fragement in the layout, and there, in onCreate, I can instantiate and reference it, no problem. The map seems to load in faster as well.
If anybody has any idea how to get this working, I'd be much obliged.
Edit: My mapfragment layout:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
tools:layout="@layout/main"/>