本文介绍了SupportMapFragment的的GetMap()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个 SupportMapFragment 动态创建,并把它放在一个的FrameLayout 容器

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

任何人都可以帮忙吗?

CenterMapFragment.java

 公共类CenterMapFragment扩展片段{

    私人SupportMapFragment mMapFragment;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){

        返回inflater.inflate(R.layout.center_map_fragment,集装箱,假);
    }

    @覆盖
    公共无效onActivityCreated(包savedInstanceState){
        super.onActivityCreated(savedInstanceState);

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


    私人无效setUpMapIfNeeded(){

        如果(mMapFragment == NULL){

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

            setUpMap();
        }
    }

    私人无效setUpMap(){

        GoogleMap的地图= mMapFragment.getMap();

        //地图为空!

    }

    @覆盖
    公共无效onResume()
    {
        super.onResume();
        setUpMapIfNeeded();
    }
}
 

center_map_fragment.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    <的FrameLayout
        机器人:ID =@ + ID /图
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent>
    < /的FrameLayout>

    <按钮
        机器人:ID =@ + ID / btn_loc
        机器人:layout_width =60dp
        机器人:layout_height =50dp
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentRight =真
        机器人:背景=@可绘制/ locbtn/>

< / RelativeLayout的>
 

解决方案

提交() FragmentTransaction 做不立即执行的操作。到时候你叫 setUpMap() onCreateView()不会一直呼吁 SupportMapFragment ,因此有将尚未是地图。

一种方法是不使用嵌套的片段,选举,而不是有 CenterMapFragment 延长 SupportMapFragment ,在这种情况下的GetMap() onCreateView应该工作的任何时间()(例如, onActivityCreated())。

I'm trying to create a SupportMapFragment dynamically and to put it in a FrameLayout container.

My issue is that mMapFragment.getMap() returns null...

Anybody can help?

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>
解决方案

commit() on a FragmentTransaction does not perform its operations immediately. By the time you call setUpMap(), onCreateView() will not have been called on the SupportMapFragment, and hence therewill not yet be a map.

One approach is to not use nested fragments, electing instead to have CenterMapFragment extend SupportMapFragment, in which case getMap() should work any time after onCreateView() (e.g., onActivityCreated()).

这篇关于SupportMapFragment的的GetMap()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 09:15