I'm having a problem loading Maps, I'm using SupportMapFragment, it does not show any visible error in the code but in Logcat it shows me the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync (com.google.android.gms.maps.OnMapReadyCallback) ' on a null object reference at com.tecnologias.uniagustapp.fragmentos.Fragment_Rutas.onViewCreated (Fragment_Rutas.java:43)
The error is generated from here:
SupportMapFragment mapFragment = (SupportMapFragment)
getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
The Fragment code is as follows:
public class Fragment_Rutas extends Fragment implements OnMapReadyCallback {
public Fragment_Rutas() {
// Required empty public constructor
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_rutas, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//SupportMapFragment mapFragment2 = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
//mapFragment2.getMapAsync((OnMapReadyCallback) getActivity());
//MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
//fragment.getMapAsync(this);
}
@Override//acercamiento
public void onMapReady(GoogleMap googleMap) {
LatLng bogota = new LatLng(4.653421, -74.145150);
googleMap.addMarker(new MarkerOptions().position(bogota)
.title("Uniagustiniana"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(bogota, 16.1f));
}
}
Next I show the XML file where I have the MapFragment tag:
<LinearLayout 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.tecnologias.uniagustapp.fragmentos.Fragment_Rutas"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#94dea7">
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="wrap_content"
android:layout_height="300dp"></fragment>
</LinearLayout>
The complete code is here:
Thank you very much for the help.