Search code examples
androidsupportmapfragment

Android Google Map in Fragment always show the SupportMapFragment was null


I want build a android map application and Launch app will got null from SupportMapFragment(said my supportMapFragment is null), if you can point me where is my mistake, please help, many thanks. code, XML and manifest as below.

By the way, the MapFragment is come from BottomNavigationView tab

        R.id.navigation_map -> {
        val mapFragment = MapFragment()
        mapFragment.setArguments(intent.extras)
        supportFragmentManager.beginTransaction()
                .add(R.id.fragment_container, mapFragment).commit()
        return@OnNavigationItemSelectedListener true

MapFragment.java

public class MapFragment extends Fragment implements OnMapReadyCallback {

    private View rootView;
    private AppCompatActivity context;
    private SupportMapFragment supportMapFragment;
    private GoogleMap map;


    public MapFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_map, container, false);

        context = (AppCompatActivity) getActivity();
        supportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.fragmentmap);

        if (supportMapFragment != null) {
            supportMapFragment.getMapAsync(this);
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, supportMapFragment).commit();
        } else {
            Toast.makeText(context, "Error - Map Fragment was null!!", Toast.LENGTH_SHORT).show();
        }
        return rootView;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

    }
}

fragment_map.xml

   <android.support.design.widget.CoordinatorLayout
        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:fitsSystemWindows="true">

        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/fragmentmap"
            tools:context=".MainActivity"
            android:name="com.google.android.gms.maps.SupportMapFragment" />
    </android.support.design.widget.CoordinatorLayout>

Manifests

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <uses-library android:name="org.apache.http.legacy" android:required="false"/>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

</application>


Solution

  • If you are using fragment inside another fragment then better to use getChildFragmentManager as it return a private FragmentManager for placing and managing Fragments inside of this Fragment.

    supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragmentmap);
    

    Reference: getChildFragmentManager