Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleMap will be null on Android API 23

I'm trying to get GoogleMap on Android API 23 but following line gives me null and that's weird, because there was no problem on API 22 or maybe last build of Google Play Services.

GoogleMap mMapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_view)).getMap();

And the layout:

<fragment
    android:id="@+id/map_view"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_marginTop="15dp"
    class="com.google.android.gms.maps.MapFragment" />

Log

09-03 07:15:52.128    4540-4540/com.XXX E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.XXX, PID: 4540, 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.XXX/com.XXX.activities.ShopNewActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.UiSettings com.google.android.gms.maps.GoogleMap.getUiSettings()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.UiSettings com.google.android.gms.maps.GoogleMap.getUiSettings()' on a null object reference
at com.XXX.activities.ShopNewActivity.onCreate(ShopNewActivity.java:211)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
like image 483
Alex Avatar asked Nov 28 '25 11:11

Alex


1 Answers

Checkout this snippet as commented by Brad:

  1. Implement OnMapReadyCallback interface.
  2. In onCreate() method add the following code to get the GoogleMap asynchronously once it's ready:

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this);

  3. Once the map is ready to be used you will get a callback thru onMapReady() in which GoogleMap is accessible.

like image 65
Sameer Khan Avatar answered Nov 30 '25 02:11

Sameer Khan