0

My app should display a map in the first fragment. The app is organised by a Navigation Drawer. The map get's displayed without a problem, BUT it doesn't show the default zoom and location I provided.

The first error I got while creating the app was a NullPointerException. I know how to fix it, so I said that my map-fragment (which is mapFragment) is not null. The app now started working, but the code I wrote in my method onMapReady as well as the Button to get the current location and move the camera to this specific location didn't show/didn't work.

I also tried changing the code for the map but I always ended up in the same spot. If I don't write map!=null I get a NullPointerException. If I do write it, the app doesn't zoom in properly to the specific location.

In the Mainactivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  SupportMapFragment mapFragment = (SupportMapFragment)
  getSupportFragmentManager()
          .findFragmentById(R.id.mapFragment);
  if(mapFragment != null) {
      mapFragment.getMapAsync(this);
  }
}

this is were I have to prevent the NullPointerException (mapFragment)


Here I set the default Location and zoom, which doesn't get applied to the map (while having the mapFragment!= null):

@Override
public void onMapReady(GoogleMap googleMap) {

    mMap = googleMap;

    if(ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) == 
       PackageManager.PERMISSION_GRANTED)
    {
        buildGoogleApiClient();

        mMap.setMyLocationEnabled(true);
    }

    LatLng Luxembourg = new LatLng(49.611622, 6.131935);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom
    (Luxembourg, 14));
}

this is the method to retrieve the user's current location (which also doesn't work while having mapFragment!= null):

@Override
public void onLocationChanged(Location location) {
    lastLocation = location;

    if(currentUserLocationMarker != null)
    {
        currentUserLocationMarker.remove();
    }

    LatLng latLng = new LatLng(location.getLatitude(), 
    location.getLongitude());

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("current user location");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker
    (BitmapDescriptorFactory.HUE_RED));

    currentUserLocationMarker = mMap.addMarker(markerOptions);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomBy(14));

    if(googleApiClient != null)
    {
        LocationServices.FusedLocationApi.removeLocationUpdates
        (googleApiClient, this);
    }
}

this is the mapFragment itself:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/mapFragment"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
</FrameLayout>

I expect the map to have the default location and zoom I provided as well as the button to move the map to the user's current location.

Reaz Murshed
  • 21,071
  • 12
  • 69
  • 87
Nondikass
  • 21
  • 8
  • If mapFragment is null then all bets are off; consider the possibility the fragment is a child of main activity layout: use `getChildFragmentManager` - see https://stackoverflow.com/a/26598640/2711811. – Andy May 21 '19 at 16:46
  • @Fabtômas thanks a lot for marking this question as a duplicate of "What is a NullPointerException, and how do I fix it?" My question is a completely different question and if you would have completely read it through, you would know what my actual problem is. I already KNOW how to fix a NullPointerException, in fact, in my code i already fixed it!! My problem is with what happens after I fix it, please read the whole question and don't judge to quickly! – Nondikass May 21 '19 at 18:19

1 Answers1

0

You might consider writing a constructor for your SupportMapFragment and initialize it first before the fragment transaction is made. Hence you will not get the NullPointerException.

I am suggesting somewhat like the following.

// Declare this in your class as a public variable.
public SupportMapFragment mapFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mapFragment = new SupportMapFragment();

  // I suppose this is doing something that requires the context. 
  // Omit this for now
  // mapFragment.getMapAsync(this);

  // Do the fragment transaction here
}

Then in the onResume method of your SupportMapFragment do the following.

@Override
public void onResume() {
    super.onResume();
    getMapAsync(getActivity());
}

This is just a sample code. You might consider passing arguments to the constructor of your SupportMapFragment which will initialize the variables necessary. I hope you get the idea.

Reaz Murshed
  • 21,071
  • 12
  • 69
  • 87