我一直试图将我的SupportMapFragment嵌套到另一个Fragment中,但是当尝试在我的SupportMapFragment对象上调用getMapAsync()时,我一直收到nullPointerException。我不知道如何解决这个问题,我已经尝试了一段时间了:/

这是我来自封闭的Fragment的onCreateView方法,该方法应该包含Map。

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

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

    CustomMap mapFrag = new CustomMap();
    FragmentTransaction transaction = this.getChildFragmentManager().beginTransaction();
    transaction.add(R.id.map_container, mapFrag).commit();

    SupportMapFragment smf = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
    smf.getMapAsync(this);

    return rootView;

}


以下是activity_main.xml文件:

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".Activities.MainActivity"
    android:focusableInTouchMode="true">

    <FrameLayout
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        app:layout_widthPercent="100%"
        android:layout_marginBottom="250dp"
        android:layout_centerInParent="true"
        android:id="@+id/map_container" />

    <EditText
        app:layout_widthPercent="90%"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/map_container"
        android:layout_alignStart="@+id/map_container"
        android:layout_marginBottom="194dp"
        android:id="@+id/editTextCode"
        android:layout_toLeftOf="@+id/buttonCode"
        android:layout_toStartOf="@+id/buttonCode"
        android:hint="Enter Unique Code" />

    <Button
        android:text="Submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/editTextCode"
        android:layout_alignRight="@+id/map"
        android:layout_alignEnd="@+id/map"
        android:id="@+id/buttonCode" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/buttonCode"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="15dp"
        android:id="@+id/myListView" />




这是我的CustomMap片段使用的map_fragment.xml文件:

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

最佳答案

尝试使用.newInstance创建新的地图片段。这是我如何使其工作的方法:

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    String MAP_FRAGMENT = "map_fragment";

    FragmentManager fragmentManager = getChildFragmentManager();
    SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager
            .findFragmentByTag(MAP_FRAGMENT); // Check if map already exists

    if(mapFragment == null){
        // Create new Map instance if it doesn't exist
        mapFragment = SupportMapFragment.newInstance();
        fragmentManager.beginTransaction()
                .replace(R.id.map_container, mapFragment, MAP_FRAGMENT)
                .commit();
    }
    mapFragment.getMapAsync(this);

07-26 03:49