0

using com.google.android.gms:play-services:9.0.0

class extend FragmentActivity

private void setMap()
            {

                map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2)).getMap();
                ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {
                    public void onMapReady(GoogleMap googleMap) {
                        map = googleMap;
                    }
                });

                if (map !=null )
                {
                    zoom = 16;

                    map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
                    map.setMyLocationEnabled(true); 
                    UiSettings uis = map.getUiSettings();
                    uis.setZoomControlsEnabled(true);  
                    uis.setMyLocationButtonEnabled(true); 
                    uis.setScrollGesturesEnabled(true); 
                    uis.setZoomGesturesEnabled(true); 



                    String snippet = "";

                    for ( int i = 0 ; i < fe.length ;i++){

                        latitude = fe[i].getEastlongitude();
                        longitude = fe[i].getNorthernlongitude();

            if (latitude== 0)
                continue;

            addr = new LatLng (latitude, longitude);
            MarkerOptions mk = new MarkerOptions();
            mk.position(addr);
            mk.title(fe[i].getTitle());
            snippet = fe[i].getAddress();
            mk.snippet(snippet);
            mk.draggable(true);
            map.addMarker(mk);
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL); // Normal MapView

            if (index == i)
            {

                address = addr;
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(addr, zoom));
            }

        }


    }
}

I get

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

enter image here

leo Zhou
  • 27
  • 1
  • 7

4 Answers4

0

If you call getMap() in onCreateView, try using onViewCreated() instead.

Try using getChildFragmentManager()

mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

Last option, you can try MapView

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

mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mapView.onResume();
mapView.getMapAsync(this);
h0102
  • 236
  • 3
  • 11
0

Hey I think you're using an invalid resource id Change the following line

((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync()

like this

((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id. fragment2)).getMapAsync()

Hope you understand the issue.

VinayagaSundar
  • 1,673
  • 17
  • 18
0

If you are talking about the onCreateView method then you must be using Fragments, according to this:

In the latest update of Google for Maps, only MapView is supported for fragments.

So, in your XML instead of using a map fragment, use a MapView:

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Then in your fragment's onCreateView:

mMapView = rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
try {
    MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
    e.printStackTrace();
}
mMapView.getMapAsync(this);

Also, according to MapView docs:

Users of this class must forward all the life cycle methods from the Activity or Fragment containing this view to the corresponding ones in this class. In particular, you must forward on the following methods:

  • onCreate(Bundle)
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onDestroy()
  • onSaveInstanceState()
  • onLowMemory()
Community
  • 1
  • 1
sebasira
  • 1,739
  • 1
  • 22
  • 41
0

Modified some fragment's setting and Using new api key is solved.

leo Zhou
  • 27
  • 1
  • 7