getSupportFragmentManager().findFragmentById returns null for google maps in fragment in android? -
i have problem google maps, i.e. getsupportfragmentmanager().findfragmentbyid returns null. have idea how solve this?
here code:
fragment_map.xml: <framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.myapp.something.mapfragment"> <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.supportmapfragment" /> </framelayout>
mapsfragment.java:
public class mapfragment extends fragment implements onmapreadycallback, android.location.locationlistener public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); supportmapfragment mapfragment = (supportmapfragment) this.getactivity().getsupportfragmentmanager().findfragmentbyid(r.id.map); mapfragment.getmapasync(this); ...
i have google maps in activity , works code:
supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager() .findfragmentbyid(r.id.map); mapfragment.getmapasync(this);
i trying reuse in fragment, becouse need maps in fragment, not in activity, doesn't work.
i tried:
- calling code in "oncreateview" function
supportmapfragment mapfragment = (supportmapfragment) getfragmentmanager().findfragmentbyid(r.id.map);
googlemap mgooglemap = ((supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.map)).getmap();
deprecated, , application crashessupportmapfragment mapfragment = (supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.map);
, similar variations, in cases null mapfragment.
do know how solve problem?
the problem you're trying use activity's fragmentmanager, , should using fragment's child fragmentmanager.
remove oncreate()
override in fragment, , add oncreateview()
override inflate layout , call getmapasync()
:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_map, container, false); supportmapfragment mapfragment = (supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.map); mapfragment.getmapasync(this); return rootview; }
Comments
Post a Comment