I want to show the current position in google maps with a marker inside a Fragment
class. The map is already shown, but I get a NullpointerException
, when I initialize the MapFragment
.
I tried it with this initialization:
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
This is the onMapReady()
function:
@Override
public void onMapReady(GoogleMap googleMap) {
this.googlemap_global = googleMap;
googlemap_global.setMyLocationEnabled(true);
googlemap_global.setTrafficEnabled(true);
googlemap_global.setIndoorEnabled(true);
googlemap_global.setBuildingsEnabled(true);
googlemap_global.getUiSettings().setZoomControlsEnabled(true);
}
And this is the layout file:
<RelativeLayout 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"
android:orientation="horizontal"
tools:context=".GPSTracker2" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="400dp"
class="com.google.android.gms.maps.MapFragment" />
<Button
android:id="@+id/gps_service_start_stop_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="GPS Service starten"/>
Here I set the Markers:
LatLng latlng = new LatLng(latitude_global,longitude_global );
if(googlemap_global != null){
Marker aktueller_standort = googlemap_global.addMarker(new MarkerOptions().position(latlng).title("Standort"));
googlemap_global.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));
// Zoom in, animating the camera.
googlemap_global.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
Here is the logcat output:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.app.Fragment android.app.FragmentManager.findFragmentById(int)' on a null object reference
at com.gpstracker.viktor.gpstracker2.Home_Fragment.<init>(Home_Fragment.java:82)
at com.gpstracker.viktor.gpstracker2.GPSTracker2.selectItemFromDrawer(GPSTracker2.java:105)
at com.gpstracker.viktor.gpstracker2.GPSTracker2.access$000(GPSTracker2.java:28)
at com.gpstracker.viktor.gpstracker2.GPSTracker2$1.onItemClick(GPSTracker2.java:67)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3066)
at android.widget.AbsListView$3.run(AbsListView.java:3903)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Thanks for helping.
Your XML File
Java file code
Show marker on map
Change this like below
to
and
to