1

I use eclipse and android sdk.I want to pin point a location on map so i use this function when user click on a button:

public void pinpointlocation(Context context){

 map = ((SupportMapFragment) ((FragmentActivity) mContext).getSupportFragmentManager().findFragmentById(android.R.id.content))
        .getMap();
 Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
            .title("Hamburg"));
        Marker kiel = map.addMarker(new MarkerOptions()
            .position(KIEL)
            .title("Kiel")
            .snippet("Kiel is cool")
            .icon(BitmapDescriptorFactory
                .fromResource(R.drawable.ic_launcher)));

        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}

but when i run the program in emulator it crash and get me these error :

04-24 06:16:35.327: E/AndroidRuntime(4070): java.lang.NullPointerException
04-24 06:16:35.327: E/AndroidRuntime(4070):     at com.example.hach.Mhelper.pinpointlocation(Mhelper.java:59)
04-24 06:16:35.327: E/AndroidRuntime(4070):     at com.example.hach.MainActivity$PlaceholderFragment$1.onClick(MainActivity.java:90)

which line 59 is the first line of the function. Why the map is null?

6
  • Did u check the same in real device Commented Apr 24, 2014 at 10:26
  • @SpringBreaker on the first line of the function. Commented Apr 24, 2014 at 10:27
  • @Ramkiran yes it crash too. Commented Apr 24, 2014 at 10:28
  • Some time if the configuration for google map is wrong then you may get this type of error. so check whether all your setting is as per the doc. Commented Apr 24, 2014 at 10:30
  • cross-check the xml and declaration of the variable: map Commented Apr 24, 2014 at 10:32

1 Answer 1

1

Please check your id in xml is the same as the one you referenced in Activity.

Quoting docs

A GoogleMap can only be acquired using getMap() when the underlying maps system is loaded and the underlying view in the fragment exists. This class automatically initializes the maps system and the view; however you cannot be guaranteed when it will be ready because this depends on the availability of the Google Play services APK. If a GoogleMap is not available, getMap() will return null.

Also check the availability of google play services before initialize map object.

Read Check for Google Play Services @

http://developer.android.com/training/location/retrieve-current.html

Sign up to request clarification or add additional context in comments.

16 Comments

the map is showing in the fragment box at the bottom of the layer,it means every thing is Okey.by everything i mean Google Play Services.
@Shombol-shagol if map is showing then what is the problem?? you should not get NPE unless id of fragment is wrong in xml
I dont know what is the problem. that is why i asked this question.it dont pinpoint.
@Shombol-shagol what happens now. The only way you get NPE is bcoz map object is null and the reason is already mentioned in the post. But you say you see the map how is that possible????????
@Shombol-shagol have you checked for google play services availability before initializing map object??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.