I have a google map in a fragment in my application. Funny thing is that the map loads, but the logcat displays a NullPointerException
MainActivity.xml :
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
MainActivity.java :
Private GoogleMap GoogleMap;
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Here's the permission:
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
And here's the metadata section:
...
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="HERE GOES YOUR API KEY" />
googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
This worked for me. getSupportFragmentManager() seemed to be causing all the crashes.
Just do like the below
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
map = googleMap;
map.setMyLocationEnabled(true);
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
}