1

what I am trying is: to show the google map with starting and end point marks in alert dialog on external activity.

So I did:

1) Create a layout, my_dialog_view.XML for a dialog:(NOTE: this layout is apart from the main layout for the activity where a dialog is called)

    <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"
    tools:context="com.example.exampleApp">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


    </RelativeLayout>

2) Define a view for the dialog layout:

    LayoutInflater factory = LayoutInflater.from(this);
    v = factory.inflate(R.layout.my_dialog_view, null);

3) Define Google map handler:

    GoogleMap gmap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

4) Set markers of starting and end point:

        LatLng startLoc = new LatLng((double)lat,(double)lng);
        CameraPosition camPos = new CameraPosition.Builder().target(startLoc)
                .zoom(15).bearing(45).tilt(70).build();
        CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(camPos);
        gmap.animateCamera(camUpd3);

        MarkerOptions markerOpts = new MarkerOptions().position(startLoc).title(
                "Start Location");
        gmap.addMarker(markerOpts);

And I have an error as follows:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference

It makes sense to have this error since the layout set for the activity (where the java code are written) does not have the fragment component that I tried to use in 3) step.

Then my question is: How to properly define a google map handler with the external dialog layout (by using LayoutInflater or any other possible solution) to resolve the null reference error?

Any input will be GREATLY appreciated.

Best,

makeasy
  • 907
  • 1
  • 6
  • 15
  • This poses a similar issue, and has been resolved, in this [thread](http://stackoverflow.com/questions/25653898/google-maps-fragment-returning-null-inside-a-fragment). – abielita Apr 05 '16 at 09:23
  • Hello, have you solved this? I'm facing the same error and what my goal is exactly this one-- how to have google maps appear in dialog. Thanks – Olufsen Apr 01 '20 at 03:33

0 Answers0