0

我遇到了(我敢肯定)愚蠢的問題,可以使用另一組眼睛。谷歌地圖片段在ActionBar標籤(與ActionBarSherlock)

我有一個應用程序需要GoogleMaps顯示在應用程序的幾個地方。我有它在我的主要活動順利所示,所有遊戲庫到位,V2關鍵是所有設置,等等

我的(截)mapview.xml樣子:

<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" /> 

正如我所說的,在主要活動上,我可以正確設置contentView並繪製地圖,沒有問題。

然而,在不同的活動中,我採取標籤:

ActionBar actionBar = getSupportActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // Add tabs 
    actionBar.addTab(actionBar.newTab().setText(R.string.map).setTabListener(this)); 

這裏就是我遇到了麻煩。在選項卡中選擇的傾聽者,我這樣做,我敢肯定是哪裏錯了(佈局是一個空的線性佈局):

GoogleMapFragment mapFragment = new GoogleMapFragment(); 
    fragmentTransaction = supportFragmentManager.beginTransaction(); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.replace(layout.getId(), mapFragment); 
    fragmentTransaction.commit(); 

那地圖片段看起來像:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.map_view, null); 
    } 

當運行時,我得到的各種誤差取決於我如何調整這一點,如

android.view.InflateException: Binary XML file line #2: Error inflating class fragment 
Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f040058, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment 

就像我說的,我敢肯定,這是錯誤的,好像我試圖誇大片段的兩倍,或者運行通過使用sa進入問題我在應用程序中的兩個位置片段。

在這一點上,我不知道如何解決它。任何建議將不勝感激。

+0

這實際上就是一個重複: http://stackoverflow.com/questions/14124354/android-supportmapfragment-with-google-map-api-2-0-giving-error – 2013-03-13 04:13:07

回答

-1

您已經在xml文件中定義了片段的佈局,因此您不必將它添加到運行時,只需在您的setContentView中傳遞片段佈局(您的案例中的mapview.xml)作爲參數活動。

+0

其實我可以沒有看到你在代碼中使用mapFragment的地方。 – Adil 2013-03-11 23:43:41

+0

對不起,有錯誤,更正。我在replace()調用中使用它。 – 2013-03-12 00:01:49

+0

和GoogleMapFragment擴展了哪些類? – Adil 2013-03-12 01:15:40

-1

嘗試在佈局中使用MapView而不是Fragment。

<com.google.android.gms.maps.MapView 
    android:id="@+id/map_view" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
+0

'MapView'在v2中已棄用;它在這裏也不起作用。但是,謝謝你的建議! – 2013-03-12 19:28:26