3

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)
Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33
Alex
  • 1,623
  • 1
  • 24
  • 48
  • Can you post the logs or more info to point out the issue? – Sameer Khan Sep 03 '15 at 15:09
  • Are you calling `findFragmentById()` in `onCreate()`? What I'm wondering is if the map is instantiated or not in `onCreate()`, which might cause `getMap()` to be `null` at this point in the lifecycle. I would try possibly moving the call to `getMap()` to the `onResume()` function. Also, if you have some logs from logcat, that would help in tracking down the issue. – Bradford2000 Sep 03 '15 at 15:17
  • 1
    Actually, [this post](http://stackoverflow.com/a/30077744/1344273) might help. It looks like the `getMap()` function is deprecated and you should use `getMapAsync()` instead. – Bradford2000 Sep 03 '15 at 15:22
  • @Bradford2000 Thanks. I think the method is deprecated but no warning supplied! I cannot test the solution right now but I will soon and let you know about the result. Thank you so much. – Alex Sep 03 '15 at 15:53

1 Answers1

2

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.

Sampada
  • 2,931
  • 7
  • 27
  • 39
Sameer Khan
  • 637
  • 6
  • 15