2016-06-01 5 views
0

In meinem aktuellen Projekt habe ich BaseActivity untergeordnete Aktivitätsfeature.Googlemap wird nicht in einem Koordinatenlayout in untergeordneten Aktivitäten angezeigt

Basis xml:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".Activity.BaseActivity"> 

<LinearLayout 
    android:id="@+id/llBody" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"></LinearLayout> 

<include layout="@layout/toolbaar_layout"/> 
</FrameLayout> 

So alle Kind-Aktivitäten werden zu diesem llbody Layout und je nach Bedarf abgerufen.

Jetzt in einer meiner Tätigkeit muss ich eine Karte zeigen.

Wenn ich eine MapFragment xml so einfach wie

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".Activity.MapsActivity"/> 

Seine Darstellung Karte wie gewohnt erstellen. Aber ich plane ein bisschen Design und so, dass ich Floatingbutton brauche, also muss ich ein Koordinatenlayout haben. Also habe ich die XML-ähnliche

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context=".Activity.BaseActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapse_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <fragment 
      android:id="@+id/map" 
      android:name="com.google.android.gms.maps.MapFragment" 
      android:layout_width="match_parent" 
      android:layout_height="300dp" 
      android:fitsSystemWindows="true" 
      app:layout_collapseMode="parallax" 
      app:layout_scrollFlags="scroll|enterAlways"/> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin"/> 

    </android.support.design.widget.CollapsingToolbarLayout> 

</android.support.design.widget.AppBarLayout> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@android:drawable/ic_dialog_email"/> 

</android.support.design.widget.CoordinatorLayout> 

Aber jetzt das Problem, dass ist

mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

MapFragment null ist, es ist nicht der Lage, die MapFragment zu finden.

Irgendein Hinweis, was schief gehen könnte? Irgendwelche Hinweise ..

Bitte helfen ..

Antwort

3

Du MapFragment zu SupportMapFragment Gießen, das Problem verursachen kann. Verwenden Sie SupportMapFragment mit AppCompat Bibliothek.

Ändern Sie das Fragment in XML als.

<fragment 
      android:id="@+id/map" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      android:layout_width="match_parent" 
      android:layout_height="300dp" 
      android:fitsSystemWindows="true" 
      app:layout_collapseMode="parallax" 
      app:layout_scrollFlags="scroll|enterAlways"/> 
1

ich Ihre gleiche XML-Datei in Java-Code verwendet, i wie this.It geändert wird, für mich zu arbeiten

GoogleMap mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(
       R.id.map)).getMap(); 

api Schlüssel in manifest

<meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="write_your_api_key_here" /> 
hinzufügen
Verwandte Themen