mapFragment.getMap() returns null

前端 未结 3 796
醉话见心
醉话见心 2020-12-20 19:40

I\'m trying to get a map from a SupportMapFragment but it returns null. From what I read this could be because the fragment is not yet fully displayed and therefore no map e

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 20:37

    From what I read this could be because the fragment is not yet fully displayed and therefore no map exists?

    Correct.

    Any ideas how to fix it?

    Actually use the layout file, by calling setContentView(), and get rid of all the FragmentTransaction stuff. You can then retrieve the already-created SupportMapFragment and use it:

    setContentView(R.layout.activity_main);
    
    SupportMapFragment mapFrag=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment);
    

提交回复
热议问题