Reputation: 193
Excuse me for any grammatical errors. I followed a tutorial to view the google map in a fragment, but something has gone wrong.
this is my file .java that is hooked with the fragment:
public class MapFragment extends Fragment implements OnMapReadyCallback{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this); //Here i get the error
}
@Override
public void onMapReady(GoogleMap googleMap) {
}
}
This is the layout fragment:
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"></fragment>
What did I do wrong?
Thank you!
Upvotes: 4
Views: 2639
Reputation: 4907
You have to use SupportMapFragment
rather than MapFragment
in XML
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"></fragment>
in Fragment
SupportMapFragment fragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
Upvotes: 6