Skip to content Skip to sidebar Skip to footer

In Google Maps V2 ... Fragment.getmap() Returns Null

I can't get the map ! all I can get is null. here is the code. public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle sav

Solution 1:

You are creating a dynamic fragment, via a FragmentTransaction. When you call commit(), the fragment has not yet been added to the screen, because the FragmentTransaction has only been scheduled to occur -- it has not occurred yet. Hence, the SupportMapFragment has not been called with onCreateView() yet, so there is no GoogleMap.

Either switch to static fragments (<fragment> tag in a layout), or delay your use of the GoogleMap until after the transaction has been processed.

Solution 2:

executePendingTransactions() in FragmentManager class was designed to fix this delay. From documantstion : After a FragmentTransaction is committed with FragmentTransaction.commit(), it is scheduled to be executed asynchronously on the process's main thread. If you want to immediately executing any such pending operations, you can call this function (only from the main thread) to do so. Note that all callbacks and other related behavior will be done from within this call, so be careful about where this is called from.

Solution 3:

I have extended the MapFragment class and added a listener. The doc about getMap() say:

... Null if the view of the fragment is not yet ready. This can happen if the fragment lifecyle have not gone through onCreateView(LayoutInflater, ViewGroup, Bundle) yet....

Then I call the listener after onCreateView. I add the class

publicclassMySupportMapFragmentextendsSupportMapFragment{

private MySupportMapFragmentListener listener;

publicinterfaceMySupportMapFragmentListener{
    publicvoidonMapCreated(GoogleMap googleMap);
}

// value taken from source codeprivatestaticfinalStringMAP_OPTIONS="MapOptions";

publicstatic MySupportMapFragment newInstance() {
        MySupportMapFragmentf=newMySupportMapFragment();
        return f;
}

publicstatic MySupportMapFragment newInstance(GoogleMapOptions options) {
        MySupportMapFragmentf=newMySupportMapFragment();
        Bundleargs=newBundle();
        args.putParcelable(MAP_OPTIONS, options);
        f.setArguments(args);
        return f;
}

// other newInstance methods .../* (non-Javadoc)
 * @see android.support.v4.app.Fragment#onViewCreated(android.view.View, android.os.Bundle)
 */@OverridepublicvoidonViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stubsuper.onViewCreated(view, savedInstanceState);
    // here, as doc say, the map can be initialized, or the service is not availableif(listener!=null){
        listener.onMapCreated(getMap());
    }

}
/**
 * @return the listener
 */public MySupportMapFragmentListener getListener() {
    return listener;
}
/**
 * @param listener the listener to set
 */publicvoidsetListener(MySupportMapFragmentListener listener) {
    this.listener = listener;
}

}

After in the calling activity or fragment ...

    mapFragment = MySupportMapFragment.newInstance();
    mapFragment.setListener(newMySupportMapFragmentListener() {
        @OverridepublicvoidonMapCreated(GoogleMap googleMap) {
            // TODO Auto-generated method stub          if(googleMap!=null){
                  // service is unaviable
            }               
        }

Solution 4:

Move the code snippet:

GoogleMap map; map = fragment.getMap();

//here map is not null

if(map!=null){

UiSettings mm = map.getUiSettings();
map.setMyLocationEnabled(true);

}

to onResume() method, this will solve your issue.

Post a Comment for "In Google Maps V2 ... Fragment.getmap() Returns Null"