I am trying to put Map inside a fragment. I am pasting the codes I am using
XML
<fragment
android:id="@+id/contactFrag_F_map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="200dp"/>
Below is my Fragment name declared
public class ContactFragment extends MapFragment
Below is how I initialised map
private void initilizeMap() {
if (googleMap == null) {
if (googleMap == null) {
googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(
R.id.contactFrag_F_map)).getMap();
}
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
And this method was called on onCreateView
The error is
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference
Please help to solve this issue. The issue is in below portion
if (googleMap == null) {
googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(
R.id.contactFrag_F_map)).getMap();
}
EDIT 1
Changed the xml to
<fragment
android:id="@+id/contactFrag_F_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="200dp"/>
and
public class ContactFragment extends SupportMapFragment
And the initialisation
if (googleMap == null) {
googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(
R.id.contactFrag_F_map)).getMap();
}
But again the error is
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
and it happens in the line
if (googleMap == null) {
googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(
R.id.contactFrag_F_map)).getMap();
}
Why would this happen??
EDIT 2
Yes.. the getmap is deprecated . So i tried
MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.contactFrag_F_map);
mapFragment.getMapAsync(this);
and ..
public class ContactFragment extends MapFragment implements OnMapReadyCallback {
and the callback ..
@Override
public void onMapReady(GoogleMap googleMap) {
theGoogleMap = googleMap;
setUpMap();
}
and the setUpMap method
public void setUpMap(){
theGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
theGoogleMap.setMyLocationEnabled(true);
theGoogleMap.setTrafficEnabled(true);
theGoogleMap.setIndoorEnabled(true);
theGoogleMap.setBuildingsEnabled(true);
theGoogleMap.getUiSettings().setZoomControlsEnabled(true);
}
And the error is showing in onresume()
java.lang.RuntimeException: Unable to resume activity {com.purpletab.daga/com.purpletab.daga.activities.CoreActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference
also
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference
The solution. I was placing a Map Fragment inside a Fragment.
So....
xml to be placed inside the fragment is
<fragment
android:id="@+id/contactFrag_F_map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="200dp"/>
and ..
public class ContactFragment extends Fragment implements OnMapReadyCallback {
with in onCreateView()
MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.contactFrag_F_map);
mapFragment.getMapAsync(this);
And on OnMapReadyCallback
@Override
public void onMapReady(GoogleMap googleMap) {
theGoogleMap = googleMap;
setUpMap();
}
and below is the setUpMap.
public void setUpMap(){
theGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
theGoogleMap.setMyLocationEnabled(true);
theGoogleMap.setTrafficEnabled(true);
theGoogleMap.setIndoorEnabled(true);
theGoogleMap.setBuildingsEnabled(true);
theGoogleMap.getUiSettings().setZoomControlsEnabled(true);
}
works fine.. :)
Don't
googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(
R.id.contactFrag_F_map)).getMap();
Do
googleMap = ((MapFragment) getActivity().getSupportFragmentmanager().findFragmentById(
R.id.contactFrag_F_map)).getMap();
getSupportFragmentManager
Return the FragmentManager for interacting with fragments associated with this activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With