2017-07-02 39 views
0

我使用的是Google地圖服務的com.google.android.gms:play-services-maps:11.0.2版本。當試圖調用下面,我得到以下錯誤:NullPointerException:試圖調用虛擬方法'void com.google.android.gms.maps.MapFragment.getMapAsync

java.lang.NullPointerException: Attempt to invoke virtual method
void

com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)」對空對象引用

下面是我的代碼:

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

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

} 
+0

'findFragmentById'返回null。查看文檔爲什麼 –

+0

@ cricket_007 這是我activity_main.xml中 「<片段的xmlns:安卓= 「http://schemas.android.com/apk/res/android」 的xmlns:地圖=「HTTP: //schemas.android.com/apk/res-auto「 xmlns:tools =」http://schemas.android.com/tools「 android:id =」@ + id/map「 android:name =」 com.google.android.gms.maps.SupportMapFragment「 android:layout_width =」match_parent「 android:layout_height =」match_parent「 tools:context =」com.example.randy.mapspaycool.MapsActivity「/>' –

+0

發佈代碼片段在這裏。你使用支持片段還是正常的? –

回答

0

看起來你正在使用SupportMapFragment代替MapFragment

改變這一行:

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

要這樣:

mapFragment = (MapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

+0

謝謝你的幫助..我解決了它.. @Sriram Kailasam –

+0

樂意幫忙:) –

+0

@Randy你可以接受答案使用旁邊的複選標記 –

相關問題