MapFragment return null

前端 未结 6 1091
Happy的楠姐
Happy的楠姐 2020-12-06 07:43
mMapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentByTag(MAP_FRAGMENT_TAG);

        // We only create a fragment if it doe         


        
6条回答
  •  天命终不由人
    2020-12-06 08:19

    Really hackish solution but, because given the poor fragment API...

    Note that I'm doing this from a custom View and accessing the Activity using (Activity)getContext()

                addOnLayoutChangeListener(new OnLayoutChangeListener(){
                    @Override
                    public void onLayoutChange(View v, int left, int top,
                            int right, int bottom, int oldLeft, int oldTop,
                            int oldRight, int oldBottom) {
    
                        GoogleMap map = mapFragment.getMap();
    
                        if (map != null) {
                             //Do stuff
                        }else{
                             removeOnLayoutChangeListener(this);
                        }
                    }
                });
    

提交回复
热议问题