0

null pointer exception when getting android google maps where is the error ?

MapActivity.java :

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

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map1))
           .getMap();

    Marker marker=map.addMarker(new MarkerOptions().position(LOCATION_CITY).title("").visible(true));
    marker.setDraggable(true);


}

xml file:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:name="com.google.android.gms.maps.MapFragment"/>
2
  • You can see the line number in LogCat.. so you tell me.. Commented Mar 1, 2014 at 12:32
  • Post your logcat. Is your Activity extends with Activity or FragmentActivity?? Commented Mar 1, 2014 at 12:33

2 Answers 2

1

you should simply check by

if (map != null) {
   Marker marker=map.addMarker(new MarkerOptions().position(LOCATION_CITY).title("").visible(true));
marker.setDraggable(true);
}

Must be sure that if your minimum SDK version is < 12 then you must use

android:name="com.google.android.gms.maps.SupportMapFragment"

and if it is >=12 then you should use this

android:name="com.google.android.gms.maps.MapFragment"
Sign up to request clarification or add additional context in comments.

Comments

0

Use SupportMapFragment instead of MapFragment like:

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

Comments

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.