1

I'm trying to show a GoogleMap in my App but I get problems right at the beginning of this task. The line:

SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

always returns null.

I have tried a lot of things but nothing works. Could you please help me to find the Problem?

Here is my Code:

public class MapSectionFragment extends Fragment implements
    ConnectionCallbacks,
    OnConnectionFailedListener,
    LocationListener,
    OnMyLocationButtonClickListener,
    OnMapReadyCallback{

    private GoogleMap map;
    Location location;
    GoogleApiClient mGoogleApiClient;

    private static final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(5000)         // 5 seconds
        .setFastestInterval(16)    // 16ms = 60fps
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    private void createMapView(){
        try{
            //here it comes
            SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

            map = mapFrag.getMap(); //throws NullPointerException
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_section_map,
            container, false);

        createMapView();

        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        mGoogleApiClient.disconnect();
    }

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

    @Override
    public void onConnected(Bundle bundle) {
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient,
            REQUEST,
            this);  // LocationListener
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map.setMyLocationEnabled(true);
    }

    @Override
    public boolean onMyLocationButtonClick() {
        Toast.makeText(getActivity(), "MyLocation button clicked", Toast.LENGTH_SHORT).show();
        // Return false so that we don't consume the event and the default behavior still occurs
        // (the camera animates to the user's current position).
    return false;
    }
}

And here is the XML "fragment_section_map" with the mapID:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">


    <Button
        android:id="@+id/refresh_map_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="@string/refresh_map" />

    <fragment
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>

Thanks for your help!

Selvin
  • 6,598
  • 3
  • 37
  • 43
Fabian K
  • 13
  • 1
  • 3
  • 1
    possible duplicate of [findFragmentById always returns null](http://stackoverflow.com/questions/10833666/findfragmentbyid-always-returns-null) – SilverCorvus Feb 20 '15 at 16:46
  • Take a look at this http://stackoverflow.com/questions/15762266/findfragmentbyid-always-returns-a-null – Danny Buonocore Feb 20 '15 at 16:46
  • 1
    obviously map fragment is not inside activitie's fragment manager ... obviously for at least 2 reasons ... first: it is child fragment of your fragment ... secondly `onCreateView` is not finished inside of, well, hmmm, `onCreateView` ... so the view is not added to activity ... so the map fragment is not added to activity .. – Selvin Feb 20 '15 at 16:56
  • @Selvin I'm new and not really good at this. Could you please explain in detail what I can do about my problem? Thanks – Fabian K Feb 20 '15 at 17:01
  • you have to learn [Fragment's lifecycle](http://developer.android.com/guide/components/fragments.html#Creating) ... in other words use different callback not onCreateView but onActivityCreated or onStart ... check the sample app for maps – Selvin Feb 20 '15 at 17:03

1 Answers1

20

please try this replace

SupportMapFragment mapFrag = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);

With

SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

because

getChildFragmentManager()

will managing fragments inside an fragment because your are using SupportMapFragment inside a fragment, so please try this solution

Hope this will help you

Ramz
  • 7,116
  • 6
  • 63
  • 88