2016-04-21 65 views
0

我一直在試圖設置一個地圖使用一些幫助,我到了這裏。所以,這是非常簡單的代碼,但崩潰。我究竟做錯了什麼?getMapAysnc(this)崩潰與空指針異常

公共類ShowDirection擴展FragmentActivity實現OnMapReadyCallback {

private GoogleMap myMap; 

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

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

    mapFragment.getMapAsync(this); // causes the Exception 


} 

@Override 
public void onMapReady(final GoogleMap map) { 
    this.myMap = map; 

    myMap.setMyLocationEnabled(true); 
} 
+1

嘗試將'this'更改爲'ShowDirection.this'並且發佈您的logcat請問 –

+3

您是否擁有id = map的片段?它導致錯誤原因mapFragment是'null' – Vucko

+0

@KISHORE_ZE應該沒有任何區別。 – Raghunandan

回答

2

仔細檢查,如果您的碎片ID實際是R.id.map,因爲它是在該行返回null。如果不是,只需用正確的id替換。