2013-06-20 47 views
0

所以我在片段類中試圖顯示一個谷歌地圖下面的代碼。即使位置檢測成功並縮放,所有其他UI組件(例如位置按鈕)也不會顯示,並且地圖對手勢沒有反應。問題是什麼?UI組件使用MapFragment不起作用?

如果我試圖在膨脹之前調用setupMap(),那麼地圖顯然還沒有準備好,我用getMap得到一個空指針。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     mMapFragment = MapFragment.newInstance(); 
     FragmentTransaction fragmentTransaction = 
       getFragmentManager().beginTransaction(); 
     fragmentTransaction.add(R.id.map, mMapFragment); 
     fragmentTransaction.commit(); 

     View v = inflater.inflate(R.layout.afragment, container, false); 

     setupMap(); 

     // Inflate the layout for this fragment 
     return v; 
    } 

我建立了我的地圖有以下幾點:

private void setupMap(){ 
     if (mMap == null) { 
      mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
           .getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       //Set Location etc 
          ........ 

       mMap.getUiSettings().setAllGesturesEnabled(true); 

       mMap.getUiSettings().setMyLocationButtonEnabled(true); 
       mMap.getUiSettings().setZoomControlsEnabled(true); 
       mMap.getUiSettings().setZoomGesturesEnabled(true); 

       // Zoom in the Google Map 
       mMap.animateCamera(CameraUpdateFactory.zoomTo(14)); 

       mMap.setOnMapClickListener(this); 

      } 
     }   
    } 

回答

0

我的解決方案是使用MapView類....

https://gist.github.com/joshdholtz/4522551

我不想改變API版本和排除其他設備(如建議由MaciejGórski),但我相信答案也是有用的,表明嵌套片段的問題。