2013-09-26 3 views
0

Я уже прочитал много тем об этой проблеме, но ничего не помогло. Итак, я хочу работать с GoogleMap в SupportMapFragment. В FragmentActivity я инициализируется карту в OnCreate методandroid (Support) MapFragment getMap() return 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 методом ... любые идеи?

€ ДИТ:

<?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

ответ

1

сталкиваются с вопросом похож, после обновления сервисов Google Play до последней версии. моя проблема была решена путем добавления метаданных в явном

<meta-data 
android:name="com.google.android.gms.version" 
android:value="@integer/google_play_services_version" /> 

https://developers.google.com/maps/documentation/android/start#add_a_map