Reputation: 2014
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
Upvotes: 10
Views: 9252
Reputation: 2014
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.. :)
Upvotes: 9
Reputation: 2608
try to use below code :
private void initilizeMap() {
FragmentManager fm = getSupportFragmentManager();
SupportMapFragment fragment = (SupportMapFragment) fm.findFragmentById(R.id.contactFrag_F_map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
}
mapView = fragment.getMap();
}
In Your Xml file:
<fragment
android:id="@+id/contactFrag_F_map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
Upvotes: 1
Reputation: 9093
First of all you're doing it all wrong from the beginning - see getMap:
This method is deprecated. Use getMapAsync(OnMapReadyCallback) instead. The callback method provides you with a GoogleMap instance guaranteed to be non-null and ready to be used.
Second - it seems (however I'm not sure) you are not familiar with fragments nesting, read this NestedFragments.
Upvotes: 0
Reputation: 576
in onCreateView
mMapView.onCreate(savedInstanceState);
mMap = mMapView.getMap();
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.setMyLocationEnabled(false);
in xml
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tag_text_header" />
Upvotes: 0
Reputation: 75788
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.
Upvotes: 5