I have a problem with Google Maps i.e. getSupportFragmentManager (). findFragmentById always returns null. Do you have an idea how to solve this?
Here is the 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 action and it works with code:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this);
I am trying to reuse this in a fragment because I need cards in the fragment and not in activity, but it does not work.
I tried:
- calling this code in onCreateView function
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap(); Deprecated and the application crashesSupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); and similar options, but in all cases I get null for mapFragment.
Do you know how I can solve this problem?
source share