2012-02-04 36 views
0

我有一個奇怪的問題,如果我在佈局中使用include,findViewById將返回null。如果mapview包含在父佈局中,findViewById將返回null null

這是我onCreate(),活動延伸MapActivity

@Override 
    public void onCreate(Bundle bundle) 
    { 
     super.onCreate(bundle); 
     setContentView(R.layout.main); 
       // this will be null 
     MapView myMapViewmyMapView = (MapView) findViewById(R.id.mapview); 

    } 

這是我的佈局,main.xml

<!-- Loads of other layout elements up here, buttons, headers etc--> 
<LinearLayout android:layout_width="fill_parent" 
        android:id="@+id/demo_mode_layout" 
        android:orientation="vertical" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:background="@drawable/panel" 
        android:layout_marginTop="5dp" 
        android:layout_marginBottom="5dp" 
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp"> 

     <!-- Include the mapview from an external mapview layout file--> 
     <include android:id="@+id/header" layout="@layout/maps"/> 
    </LinearLayout> 

和Maps查看我引用:

<?xml version="1.0" encoding="utf-8"?> 
<com.google.android.maps.MapView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/mapview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:clickable="true" 
     android:apiKey="my legit key here" 
     /> 

如果在我的活動中,我使用了內容視圖R.layout.main它會失敗,但如果我將它設置爲地圖實體,R.layout.maps那麼一切都很好。

我想使用R.layout.main,所以我可以使視圖具有我的頁眉,頁腳,按鈕菜單等,爲什麼這不起作用?

+0

如果你''東西*其他*比'MapView',它的工作原理? – CommonsWare 2012-02-04 18:28:46

+0

在包含標籤中使用'android:id''會覆蓋包含的佈局文件中的根元素標識。你可以簡單地刪除它,如果你沒有使用''id/header''。 – harism 2012-02-04 18:31:33

+0

在Android開發的例子膨脹視圖 - 東西要嘗試? – AJcodez 2012-02-04 18:53:37

回答