1

Im trying to find my Coordinates and show it in the Map fragment with this code but my app is crashing. Im using the GoogleMap API for it which is up to date.

public class gmapp extends FragmentActivity{

// Google Map
private GoogleMap googleMap;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    setUpIfNeeded();
}

private void setUpIfNeeded() {
    if (googleMap == null)
    {
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    if (googleMap != null)
    {
        setUpMap();
    }

  }
}

private void setUpMap() {

    googleMap.setMyLocationEnabled(true);

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria , true);
    Location myLocation = locationManager.getLastKnownLocation(provider);
    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    double latitude = myLocation.getLatitude();
    double longtitude = myLocation.getLongitude();
    LatLng latLng = new LatLng(latitude , longtitude);
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
    googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude , longtitude)).title("Im here"));

}

}

Here's my error Message after I compile it.

    09-12 20:10:01.034: E/AndroidRuntime(27507): FATAL EXCEPTION: main
09-12 20:10:01.034: E/AndroidRuntime(27507): Process: com.max.maxo, PID: 27507
09-12 20:10:01.034: E/AndroidRuntime(27507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.max.maxo/com.max.maxo.gmapp}: java.lang.NullPointerException
09-12 20:10:01.034: E/AndroidRuntime(27507):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at android.app.ActivityThread.access$900(ActivityThread.java:172)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at android.os.Looper.loop(Looper.java:136)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at android.app.ActivityThread.main(ActivityThread.java:5586)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at java.lang.reflect.Method.invokeNative(Native Method)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at java.lang.reflect.Method.invoke(Method.java:515)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at dalvik.system.NativeStart.main(Native Method)
09-12 20:10:01.034: E/AndroidRuntime(27507): Caused by: java.lang.NullPointerException
09-12 20:10:01.034: E/AndroidRuntime(27507):    at com.max.maxo.gmapp.setUpIfNeeded(gmapp.java:35)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at com.max.maxo.gmapp.onCreate(gmapp.java:29)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at android.app.Activity.performCreate(Activity.java:5451)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
09-12 20:10:01.034: E/AndroidRuntime(27507):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
09-12 20:10:01.034: E/AndroidRuntime(27507):    ... 11 more

In my XML I only got the fragment map

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Ahmet K
  • 713
  • 18
  • 42
  • `findFragmentById(R.id.map)` is probably null i.e. it can't find your fragment in your layout. Have you declared it correctly in XML? – Blundell Sep 12 '14 at 18:18
  • It is , added into first post – Ahmet K Sep 12 '14 at 18:21
  • Just to make sure that the map is ready when you access it, I'd do what's suggested [here](http://stackoverflow.com/questions/14047257/how-do-i-know-the-map-is-ready-to-get-used-when-using-the-supportmapfragment) (set a callback for when the fragment is actually created). – mbmc Sep 12 '14 at 18:50

1 Answers1

1

I found it ! My problem was that I forgot to add

 class="com.google.android.gms.maps.SupportMapFragment"

In my map XML File . Hopefully this will help someone

Ahmet K
  • 713
  • 18
  • 42