42556

Google map v2 android method getMap() returns null

Question:

In my application I am using google map programmatically, but when I call getMap() it returns null. even I have tried onActivityCreated() but still it returns null. somebody please help me.. Here is my code,

public void callMap() { try { int status = GooglePlayServicesUtil .isGooglePlayServicesAvailable(mActivity); if (status == ConnectionResult.SUCCESS) { Log.d("sreedhu", "Google Play Service Available"); gmo = (new GoogleMapOptions()).zoomControlsEnabled(true) .rotateGesturesEnabled(true); mapFragment = SupportMapFragment.newInstance(gmo); map=mapFragment.getMap(); manager=getFragmentManager(); FragmentTransaction fragmentTransaction = manager .beginTransaction(); fragmentTransaction.add(R.id.mapFragmentHole, mapFragment); fragmentTransaction.commit(); manager.executePendingTransactions(); mapFragment=getMap(); } else if (GooglePlayServicesUtil.isUserRecoverableError(status)) { ErrorDialogFragment.newInstance(status).show( getFragmentManager(), "errorDialog"); } else { Toast.makeText(mActivity, "Google Map v2 not available", Toast.LENGTH_LONG).show(); mActivity.popFragments(); } } catch (Exception e) { Log.d("sreedhu", "play" + e.toString()); } }

Answer1:

executePendingTransactions seems not work like you expect it.

When MapFragment is created from code, you won't get GoogleMap from it until its onCreateView is called.

You need to call map=mapFragment.getMap(); and all code after it in onStart or onResume.

Answer2:

Finally, I got the fix for this problem...

I include this below piece of snippet on onCreateView which solved my problem,

SupportMapFragment mFragment; mFragment = new SupportMapFragment() { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if ((map = mFragment.getMap()) != null) { setUpMap(); } } };

Once the map is prepared the SuppotMapFragment's onActivityCreated will get called there you can setup your map.

Recommend

  • How to solve java.lang.NoClassDefFoundError for com.google.android.gms.R$styleable error?
  • Query tangled array in Pymongo
  • Yahoo-Finance module doesn't work anymore? [duplicate]
  • Get the clientWidth and clientHeight of resized image
  • Execute compiled ClojureScript from the commandline with Rhino
  • how to use maps fragment in navigation drawer?
  • using xpaths in javascript
  • HashMap not returning image to marker by onMarkerClick
  • Failed: Cannot read property 'getWebElements' of undefined
  • Protractor UI Tests fail with Jenkins, successful on WinServer on cmd run
  • Google map v2 android method getMap() returns null
  • Using google maps in android
  • replace, remove fragment and removing view doesn´t work in second run
  • 400 Bad Request Required: X-Goog-Encode-Response-If-Executable
  • Display Dialog from Chrome Extension
  • How to create an element using values of already existed elements in a dict, inside a list?
  • Selenium: Unable to Select DropDown
  • save gps location from “onMyLocationChange” function which is inside onMyLocationChange function
  • How to refresh the Marker on the Map?
  • How to toast a message for a specific time period?
  • Adding Multiple Markers Google Maps
  • Adding Multiple Markers Google Maps
  • Map fragment is black after resume
  • How to handle click event component of custom marker in android
  • Google Closure Compiler advanced: remove code blocks at compile time
  • Web scraping information other than price from Yahoo Finance in Python 3
  • Read data from a txt file without downloading it?
  • Why is my event listener written in Google Closure not working?
  • How to disable jQuery.jplayer autoplay?
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?