Null对象引用上的SupportMapFragment#getMapAsync()

时间:2017-04-02 16:21:11

标签: java android android-fragments nullpointerexception

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

这是来自封闭片段的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" />

1 个答案:

答案 0 :(得分:3)

尝试使用.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);