0

I am trying to convert an activity to a fragment and have problems with getFragmentManager.

my xml file:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="xpak.probandosidebar.MapsFragment"
class ="com.google.android.gms.maps.SupportMapFragment" />

and my java file:

public class MapsFragment extends Fragment {

private GoogleMap mMap;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_maps,container,false);
}

@Override
public void onStart() {
    super.onStart();
    setUpMapIfNeeded();
}

@Override
public void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}


private void setUpMapIfNeeded() {
    if (mMap == null) {
        //here is the error
       mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();

        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}}

the error is: cannot cast android.app.Fragment to com.google.android.gms.maps.SupportMapFragment

I searched everywhere without success , I hope you can help me

greetings from chile

Diego Said
  • 31
  • 7

1 Answers1

0

Try doing this

<com.google.android.gms.maps.SupportMapFragment

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="xpak.probandosidebar.MapsFragment"
class ="com.google.android.gms.maps.SupportMapFragment" />
igorshkov
  • 3
  • 4