0

我正在嘗試在片段尋呼機的活躍度中使用Google地圖,因此地圖站在尋呼機後面並仍然能夠與它交互,但是無論我做什麼,我都會收到null例外。在AppCompatAcivity中調用mapfragment

我曾嘗試使用SupportMapFragment就象這樣:

SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.Map)); 
     mapFragment.getMapAsync(this); 

但getChildFragmentManager不能因爲解決的是AppCompatActivity我猜。 有沒有另外一種方法呢?可能嗎?謝謝。

+0

地圖碎片就變成getSupportFragmentManager() –

回答

3

嘗試動態添加它。

 MapFragment mMapFragment = MapFragment.newInstance(); 
     FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
     fragmentTransaction.add(R.id.llMapContainer, mMapFragment, "map"); 
     fragmentTransaction.commit(); 

,否則你可以得到這樣

 mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)); 
     if (mapFragment != null) { 
      mapFragment.getMapAsync(new OnMapReadyCallback() { 
       @Override 
       public void onMapReady(GoogleMap map) { 
        loadMap(map); 
       } 
      }); 
     } 
+0

謝謝!我做了它動態和工作得很好:D –