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

标签 java android android-fragments nullpointerexception

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

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

@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 Fragment 使用的 ma​​p_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 创建一个新的 map fragment 。这是我如何让它工作的:

    // 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);

关于java - Null 对象引用上的 SupportMapFragment#getMapAsync(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43170615/

相关文章:

Java RESTful Web 服务,404 未找到

android - 与 Volley 库和服务的长时间联网操作

Android vitals - 卡住唤醒锁和前台服务

android:configChanges ="orientation"不适用于 fragment

java - Android - 如何结束 fragment

java - 如何用enum进行分类

java - Java 编译器是否优化了循环局部变量的创建?

android - 如何禁用选项卡布局中的默认选择?

android - 带有滑动菜单和操作栏的动态 UI

java - 如何调用我在某人链接到的 wsdl 文件中看到的方法?