2013-05-13 41 views
0

我試圖獲得並使用下面的代碼設置我的GoogleMap的變量:谷歌地圖變量爲空

private GoogleMap mMap; 

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap(); 

    //MapFragment fm = (MapFragment)  getFragmentManager().findFragmentById(R.id.myMapView); 
    //mMap = fm.getMap(); 

    Log.e("RideTracking", "Google Map VALUE:"+mMap); 

    if (mMap != null) { 
     proxy.setProjection(mMap.getProjection()); 
    } 

這裏是我的應用程序,這部分XML:

<fragment 
     android:id="@+id/myMapView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.MapFragment"/> 

由於某種原因,mMap的值爲零,我不確定它爲什麼返回null,因爲我有這行代碼:

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap(); 

這裏是我得到的logcat輸出:

05-13 14:09:08.553: E/RideTracking(6415): Google Map VALUE:null 

任何人都可以提供任何意見,爲什麼mMap爲null?任何幫助,將不勝感激。

+0

你可以發佈清單文件嗎? – Raghunandan 2013-05-13 19:35:56

+0

你是指你的android地圖項目中的谷歌播放服務庫項目?檢查清單中的地圖密鑰 – Raghunandan 2013-05-13 19:45:00

回答

0

如果試着把你的代碼放在方法onActivityCreated()。這可以確保您的地圖已準備就緒。

0

工作,我有同樣的問題,然後我發現我忘了寫行

setContentView(R.layout.activity_direction);

然後我的地圖工作正常。所以請確保在聲明地圖之前設置了佈局。