2013-09-26 40 views
0

我已經讀了很多關於這個問題的主題,但沒有任何幫助。 所以我想在SupportMapFragment的GoogleMap上進行操作。在FragmentActivity我初始化在onCreate方法android(支持)MapFragment getMap()返回null

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_frame); 

    // Initialize Google Maps Android API 
    try { 
     MapsInitializer.initialize(this); 
    } catch (GooglePlayServicesNotAvailableException e) { 
     e.printStackTrace(); 
    } 
... 

地圖我MapFragment的代碼看起來完全一樣

public class MapFragment extends SupportMapFragment { 

private final String TAG = getClass().getSimpleName(); 


public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.map, null); 
} 

public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    GoogleMap gMap = this.getMap(); 

    Log.d(TAG, 
      "getMap() " + ((gMap == null) ? "null" : gMap.toString())); 

    // Setting Location and Zoom 
    gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
      40.76793169992044f, -73.98180484771729f), 14.0f)); 
} 

} 

每當我得到空與GetMap操作方法...任何想法?

€DIT:

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:map="http://schemas.android.com/apk/res-auto" 
android:id="@+id/map" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
class="com.google.android.gms.maps.SupportMapFragment" 
map:cameraBearing="112.5" 
map:cameraTargetLat="-33.796923" 
map:cameraTargetLng="150.922433" 
map:cameraTilt="30" 
map:cameraZoom="50" 
map:mapType="normal" 
map:uiCompass="false" 
map:uiRotateGestures="true" 
map:uiScrollGestures="false" 
map:uiTiltGestures="true" 
map:uiZoomControls="false" 
map:uiZoomGestures="true" /> 
+1

添加元數據解決的是有你的佈局的地圖嗎? – njzk2

+0

不是直接。我有一個片段佈局文件。在編輯的文章中看到它。 – Forke

+1

您是否嘗試將您的getMap()代碼移動到onStart或onResume?看到我的回答http://stackoverflow.com/a/20747717/2364687,雖然它可能不適用於你的情況。此外,你一定會安裝Google Play服務(即你沒有捕獲並忽略onCreate中的異常)? – GLee

回答