0

Can someone tell me why my getMapAsync() returns a nullpointerexception? And how to resolve it. I have removed my setContentView() and used the onCreateView(), so normally my view should inflate, no?

Thanks for the help!

I get the following error from my main code:

Process: com.example.flow, PID: 9070
    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.example.flow.displayClasses.WebscrapingScreens.GoogleMapsFragment.onCreate(GoogleMapsFragment.java:100)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:2414)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
        at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)
        at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)
        at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)

This is my Fragment :

public class GoogleMapsFragment  extends Fragment implements OnMapReadyCallback, LocationResultListener, PlaceSelectedListener {

    private static final int ACTIVITY_RQEUEST_CODE = 1000;
    private static final int PERMISSION_REQUEST_CODE = 1000;
    private static final float ZOOM_LEVEL = 15.0f;
    private static final int REQUEST_LIMIT = 3;

    private final String API_KEY = "MyKey";
    private final String PLACES_REQUEST = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?&key=" + API_KEY;

    private GoogleMap googleMap;
    private ClusterManager<MarkClusterItem> clusterManager;

    private LocationHandler locationHandler;

    private int requestCount;
    private String nextPageToken;

    private ProgressBar progressBar;

    private List<GooglePlace> listPlaces = new ArrayList<>();

    private FragmentActivity myContext;

    public GoogleMapsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Activity activity) {
        myContext=(FragmentActivity) activity;
        super.onAttach(activity);
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View RootView = inflater.inflate(R.layout.fragment_mapsgoogle, container, false);

        return RootView;}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        progressBar = getActivity().findViewById(R.id.progressBar);

        locationHandler = new LocationHandler(this, this, ACTIVITY_RQEUEST_CODE, PERMISSION_REQUEST_CODE);

        SupportMapFragment supportMapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.googleMap);
        supportMapFragment.getMapAsync(this);
    }
Inyea
  • 47
  • 1
  • 7
  • If your `` element for the `SupportMapFragment` is in the `fragment_mapsgoogle` layout, you need to use `getChildFragmentManager()` to find it, not the `Activity`'s `FragmentManager`. – Mike M. Dec 09 '18 at 20:36
  • I've tried that solution but I still get the same error.. @MikeM. – Inyea Dec 09 '18 at 21:27
  • Are you sure you have the right class name on the `` element inside the `fragment_mapsgoogle` layout? It must be `com.google.android.gms.maps.SupportMapFragment`, not `com.google.android.gms.maps.MapFragment`. – Mike M. Dec 09 '18 at 21:30
  • Oh, wait, I see it. You've got that setup in `onCreate()`, which runs _before_ `onCreateView()`. Move the map stuff into `onActivityCreated()` instead. – Mike M. Dec 09 '18 at 21:32
  • You made my day sir! Would you mind telling me why I had to put it in onActivityCreated? @MikeM. – Inyea Dec 09 '18 at 21:45
  • The `onCreate()` method runs _before_ `onCreateView()`. You haven't yet inflated and returned your `View` in `onCreate()`, so the child `FragmentManager` didn't have your `SupportMapFragment` yet. The `onActivityCreated()` method runs after `onCreateView()`, so your `Fragment`'s all ready and set by then. – Mike M. Dec 09 '18 at 21:50
  • There's a diagram on [this developer page](https://developer.android.com/guide/components/fragments#Creating) that covers most of `Fragment`'s lifecycle methods. Might be of some help to you. I'll update the duplicate links with another that explains this particular issue, when I get a chance later. Glad you got it working. Cheers! – Mike M. Dec 09 '18 at 21:53

0 Answers0