Android API 23上的GoogleMap将为null

时间:2015-09-03 14:48:20

标签: android google-play-services google-maps-android-api-2

我试图在Android API 23上获取GoogleMap,但是后续行给了我null并且这很奇怪,因为在API 22或者Google Play服务的最后版本上没有问题

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

布局:

<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" />

日志

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)

1 个答案:

答案 0 :(得分:2)

通过Brad评论此片段:

  1. 实施OnMapReadyCallback界面。
  2. onCreate()方法中添加以下代码,以便在GoogleMap准备好后异步获取它:

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

  3. 一旦地图准备就绪,您就可以通过onMapReady()获得回访,其中可以访问GoogleMap。