onMapReady not being called: Google Map in FrgmentActivity: Android Studio -
i have fragmentactivity in trying load google map. , implements onmapreadycallback , when try inflate map mapfragment object app crashes after getmapasync called. below code:
public class mapactivity extends fragmentactivity implements onmapreadycallback { private googlemap googlemap; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_map); initializemap(); } private void initializemap() { if (googlemap == null) { supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); if(mapfragment!=null){ mapfragment.getmapasync(mapactivity.this); } // check if map created or not if (null== googlemap) { toast.maketext(getapplicationcontext(), "sorry! unable create maps", toast.length_short).show(); } } } @override public void onmapready(googlemap map) { googlemap = map; googlemap.setmylocationenabled(true); } }
i tried degug , noticed getting mapfragment object never reaches onmapready method makes googlemap object null , app crashes when method pertaining googlemap(e.g. setoncamerachangelistener) being called. have gone through documentation page doesn't seem work in project.
is there might missing in project? how should overcome error , stop app crash.
tia
edit 1:
below map fragment in xml file
<fragment android:id="@+id/map" class="com.google.android.gms.maps.supportmapfragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mapsactivity" android:name="com.google.android.gms.maps.supportmapfragment"/>
the hint getmapasync
(asynchronous).
onmapready
not called straight away, meaning googlemap
won't set until method called.
edit: make sure aren't doing methods call on googlemap
before set map value, i.e. add check if null before running method on it. add @nullable
annotation private googlemap googlemap;
aid in development android studio alert if missing null check.
Comments
Post a Comment