Question

I've got a strange issue in that findViewById will return null for anything, if I use an include in my layout.

This is my onCreate(), the activity extends 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);

    }

And this is my layout, 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>

And the maps view I'm referencing :

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

If in my activity, I set the content view using R.layout.main it will fail, but if I set it to just the maps entity, R.layout.maps then everything is perfectly fine.

I want to use R.layout.main so I can skin the view to have my headers, footers, buttons menus etc, so why doesn't this work?

Was it helpful?

Solution

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

Overrides root view id in included layout if it's been set. In this case MapView id is replaced with this new one.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top