与navigationDrawer SupportMapFragment [英] SupportMapFragment with navigationDrawer
问题描述
我有延伸ActionBarActivity并具有抽屉式导航栏中的活动。这是该活动的XML布局:
I have an activity that extends ActionBarActivity and has a navigation drawer. This is the xml layout of the activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="mypackage.utils.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer"/>
</android.support.v4.widget.DrawerLayout>
然后,我有地图片段:
And then I have a fragment with the map:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity$PlaceholderFragment">
<fragment
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<ImageButton
android:onClick="showUserPosition"
android:background="@drawable/selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
当我选择从地图片段贴在容器抽屉地图部分,我需要一个实例化GoogleMap对象。我试着用:
When I choose the Map section from the drawer the map fragment is attached to the container and I need to instanciate a GoogleMap object. I tried with:
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
和
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.container)).getMap();
但始终得到NullPointerException异常。我知道这是不是执行时间的问题,因为我甚至试图实例化的GoogleMap的钮,我pressed长期地图显示后的onClick()方法。我怎样才能让我的地图参考?
but always got NullPointerException.. I know it's not a problem of execution time because I even tried to instantiate the GoogleMap in a botton onClick() method that I pressed long after the map showed. How can I get my map reference?
推荐答案
最后,我设法得到它与这些三线code的工作:
Finally I managed to get it work with these 3 lines of code:
Fragment f = getSupportFragmentManager().findFragmentById(R.id.container);
SupportMapFragment mapFragment = (SupportMapFragment) f.getChildFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = mapFragment.getMap();
这篇关于与navigationDrawer SupportMapFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!