SupportMapFragment's getMap() returns null

只愿长相守 提交于 2019-12-01 03:15:11

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 there will 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()).

In the following link MapView is used as CommonsWare suggests in the last part of his comment:

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();
    }
}

I hope these will be helpful.

Calling

fragmentTransaction.commit();

is asynchronous, the map won't be ready yet when you return from it. Simply call

getFragmentManager().executePendingTransactions();

as the next line, that will make it synchronous and will execute it for the next instruction to pick up the finished map all right. If you're worried about the time it takes, put the whole initialization into an AsyncTask and show a progress indicator to the user while the map is initializing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!