Fragment中的Android Google Map始终显示SupportMapFragment为空

时间:2018-10-30 07:08:36

标签: android supportmapfragment

我想构建一个Android地图应用程序,Launch应用程序将从SupportMapFragment获取空值(表示我的supportMapFragment为null),如果您可以指出我的错误所在,请帮忙,非常感谢。 代码,XML和清单如下。

顺便说一下,MapFragment来自BottomNavigationView选项卡

        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>

清单

                   

<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>

1 个答案:

答案 0 :(得分:1)

如果您正在另一个片段中使用片段,则最好使用 getChildFragmentManager ,因为它返回一个私有FragmentManager,用于在此Fragments内放置和管理Fragment

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

参考:getChildFragmentManager