title: layout as listview header
body: <p>I have a layout that contains two lists. Since you can't put lists in a <code>ScrollView</code> I am trying to use an <code>ExpandableListView</code>, with a custom <code>SimpleExpandableListAdapter</code>, as per <a href="https://stackoverflow.com/questions/1353101/about-expandable-list-in-android">this question</a>.<br>
  List layout:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent" android:layout_height="fill_parent"&gt;
      &lt;ExpandableListView android:id="@+id/android:list"
          android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Now, the troubles come when I try to add something as header. It works if I create a layout from code, like this:  </p>
  
  <pre><code>LinearLayout ll = new LinearLayout( this );
  ll.setOrientation( LinearLayout.VERTICAL );
  TextView t = new TextView( this );
  t.setText( "some text" );
  ll.addView( t );
  ImageView i = new ImageView( this );
  i.setImageResource( R.drawable.icon );
  ll.addView( i );
  getExpandableListView( ).addHeaderView( ll );
  </code></pre>
  
  <p>If I use the same layout, or simpler even than the one from code, the application crashes before even showing on screen. Here's the layout I want to add as header to the above list:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:orientation="vertical" android:id="@+id/ll"&gt;
          &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
          android:text="some text"/&gt;
          &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
          android:src="@drawable/icon"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And the code:</p>
  
  <pre><code>LinearLayout ll = (LinearLayout)findViewById( R.id.ll );
  getExpandableListView( ).addHeaderView( ll );
  </code></pre>
  
  <p>I tried to fiddle with the layout_width or height of about all the components in my layout, but it doesn't work. What baffles me most is that when it crashes, on the DDMS says <code>NullPointerException</code> when calling setAdapter (which doesn't when I use the layout from code, and it adds the adapter just fine):</p>
  
  <pre><code>01-16 01:17:27.447: ERROR/AndroidRuntime(21314): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.stephan.test/org.stephan.test.ExpandableListViewTest}: java.lang.NullPointerException
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2833)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread.access$2300(ActivityThread.java:136)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.os.Handler.dispatchMessage(Handler.java:99)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.os.Looper.loop(Looper.java:143)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread.main(ActivityThread.java:5068)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at java.lang.reflect.Method.invoke(Method.java:521)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at dalvik.system.NativeStart.main(Native Method)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314): Caused by: java.lang.NullPointerException
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.widget.ListView.clearRecycledState(ListView.java:506)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.widget.ListView.resetList(ListView.java:492)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.widget.ListView.setAdapter(ListView.java:424)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:475)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ExpandableListActivity.setListAdapter(ExpandableListActivity.java:246)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at org.stephan.test.ExpandableListViewTest.onCreate(ExpandableListViewTest.java:80)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1066)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2797)
  01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     ... 11 more
  </code></pre>
  
  <p>So, my question is: what am I doing wrong here? And, in case, any advices on how I could do this better?</p>