0

我有一个奇怪的问题findViewById,如果我在布局中使用包含,任何东西都会返回 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>

我引用的地图视图:

<?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这样我就可以为视图设置页眉、页脚、按钮菜单等,为什么这不起作用?

4

1 回答 1

3
<include android:id="@+id/header"..>

如果已设置,则覆盖包含布局中的根视图 ID。在这种情况下,MapView id 将替换为这个新的。

http://developer.android.com/guide/topics/resources/layout-resource.html#include-element

于 2012-02-04T19:22:36.900 回答