3

I am currently trying to develop an Android app with google Maps integration. At the moment I have difficulties to find the error, because the code is from the google site itself, except that it is a SupportMapFragment. If you know an actual tutorial with SupportMapFragment would also be great. Actual, because I think .getMap() is depreciated. If I should post the whole code, let me know (I am new in stackoverflow)

If try to use this code:

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback {

where a function is:

    @Override
     public void onResume() {
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
            .findFragmentById(R.id.map);

    supportMapFragment.getMapAsync(this);
}

The error is: 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

The Fragment xml looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>
Charlie
  • 1,169
  • 2
  • 16
  • 31

2 Answers2

7

It seems like you need to use the Fragment's SupportFragmentManager See the answer to this findFragmentById for SupportMapFragment returns null in Android Studio

Community
  • 1
  • 1
puj
  • 770
  • 5
  • 9
7

Map fragment is acting up on Lollipop you may need this instead.

SupportMapFragment supportMapFragment;
if (Build.VERSION.SDK_INT < 21) {
    supportMapFragment = (SupportMapFragment) getActivity()
        .getSupportFragmentManager().findFragmentById(R.id.map);
} else {
    supportMapFragment = (SupportMapFragment) getActivity()
        .getChildFragmentManager().findFragmentById(R.id.map);
}
supportMapFragment.getMapAsync(this);
JJD
  • 50,076
  • 60
  • 203
  • 339
Xjasz
  • 1,238
  • 1
  • 9
  • 21
  • This Code helped, but the App still crashes. With this Error: `java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.c.ey.o()' on a null object reference` – Charlie Mar 19 '15 at 16:30
  • this is the exact error I am currently getting - " 'void com.google.maps.api.android.lib6.c.ey.o()' on a null object reference" – Sambuxc Apr 08 '15 at 16:49