2013-07-29 79 views
2

我正在实现和Android应用程序,我试图在另一个片段中使用SupportMapFragment。 地图片段工作正常,我可以看到地图,我没有得到任何错误。 但是,在主要活动中,我试图从嵌套的片段获取GoogleMap对象,但是我得到的全部为空。任何想法我怎么能克服这一点?SupportMapFragment嵌套在另一个片段

main_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" /> 

片段layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#E8E8E8"> 

     <fragment 
      android:id="@+id/map" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      android:layout_width="match_parent" 
      android:layout_height="117dp" 
      android:layout_margin="5dp" /> 
<LinearLayout/> 
在MainActivity.java::onCreate()

// Getting reference to the SupportMapFragment of activity_main.xml 
     SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 

     //fm is null here 
+0

你的活动类是否扩展'FragmentActivity'? – Raghunandan

+0

@Raghunandan yes,.... extends FragmentActivity implements \t \t ActionBar.TabListener { – maephisto

+0

您可以发布堆栈跟踪。 – Raghunandan

回答

1

目前还不清楚,你实际上是在这里有“嵌套片段”。我只看到一个片段:SupportMapFragment

如果您ViewPager是片段里面,你FragmentPagerAdapter使用SupportMapFragment,那么你需要在孩子FragmentManager,即FragmentPagerAdapter使用一个被调用findFragmentById()

如果您的ViewPager不在片段内,那么我不认为您有嵌套片段。但是,您的ViewPager可能尚未实例化您的整体活动的onCreate()中的任何页面,因此您的片段尚不存在。等到您的FragmentPagerAdapter已创建您的SupportMapFragment,,然后尝试使用它。