SupportMapFragment的getMap()返回null。

10

我正在尝试动态创建一个SupportMapFragment并将其放置在一个FrameLayout容器中。

我的问题是mMapFragment.getMap()返回null...

有人能帮忙吗?

CenterMapFragment.java

public class CenterMapFragment extends Fragment {

    private SupportMapFragment mMapFragment;

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

        return inflater.inflate(R.layout.center_map_fragment, container, false);
    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) == ConnectionResult.SUCCESS) {
            setUpMapIfNeeded();
        }
    }


    private void setUpMapIfNeeded() {

        if (mMapFragment == null) {

            mMapFragment = SupportMapFragment.newInstance();
            FragmentTransaction fragmentTransaction =
                            getChildFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.map, mMapFragment);
            fragmentTransaction.commit(); 

            setUpMap();
        }
    }

    private void setUpMap() {       

        GoogleMap map = mMapFragment.getMap();  

        // map is null!

    }

    @Override
    public void onResume()
    {
        super.onResume();
        setUpMapIfNeeded();     
    }
}

center_map_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <Button
        android:id="@+id/btn_loc"
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/locbtn" />

</RelativeLayout>
3个回答

8
FragmentTransaction 上的 commit() 方法不会立即执行其操作。在调用 setUpMap() 时,onCreateView() 尚未在 SupportMapFragment 上调用,因此还没有地图。

一种方法是不使用嵌套片段,而是选择让 CenterMapFragment 扩展 SupportMapFragment,在这种情况下,getMap() 应该在 onCreateView() 之后的任何时间都可以工作(例如: onActivityCreated())。


如果CenterMapFragment扩展了SupportMapFragment,我该如何将地图设置到FrameLayout中?我需要更改布局吗? - jul
1
@jul:一种方法是让CenterMapFragment实现一个onCreateView(),首先调用super.onCreateView(),以获取SupportMapFragment创建的任何内容。然后它将创建FrameLayout并将super创建的View作为FrameLayout的子项添加进去。然后从onCreateView()返回FrameLayout。这样可以将SupportMapFragment的内容包装在CenterMapFragment想要的内容中。或者,让CenterMapFragment成为普通的Fragment,使用MapView而不是SupportMapFragment。或者,找到更好的时间来使用你当前的代码调用setUpMap() - CommonsWare
@CommonsWare 的第一种方法比直接处理 MapView 更容易。MapView 要求你将所有生命周期方法都转发给它,并自己运行 MapsInitializer.initialize(...) 代码。 - Mason Lee
扩展 SupportMapFragment 并从 onCreateView() 调用 super 不起作用。仍然出现 NullPointerException - Muhammad Babar

2
以下链接中使用了MapView,正如CommonsWare在其评论的最后部分所建议的那样: http://ucla.jamesyxu.com/?p=287
public class MapFragment extends Fragment {

    MapView m;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        // inflat and return the layout
        View v = inflater.inflate(R.layout.map_fragment, container, false);
        m = (MapView) v.findViewById(R.id.mapView);
        m.onCreate(savedInstanceState);

        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        m.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        m.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        m.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        m.onLowMemory();
    }
}

我希望这些对您有所帮助。

1

调用

fragmentTransaction.commit();

由于异步的原因,当您从中返回时,地图尚未准备好。只需调用即可。
getFragmentManager().executePendingTransactions();

作为下一行,这将使其同步并执行下一条指令以捕获完成的地图。如果您担心所需时间,请将整个初始化放入AsyncTask中,并在地图初始化时向用户显示进度指示器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接