title: ExpandableListView with a header in extended LinearLayout: NullPointerException
body: <p>I have defined a simple custom <code>View</code> by extending <code>LinearLayout</code>. The crux is that it contains an <code>ExpandableListView</code> with a header view added:</p>
  <pre><code>public class MyView extends LinearLayout {
  private View mHeaderView;
  private ExpandableListView mListView;
  
  @SuppressWarnings(&quot;UnusedDeclaration&quot;)
  public MyView(Context context) {
      super(context);
      init(context);
  }
  
  @SuppressWarnings(&quot;UnusedDeclaration&quot;)
  public MyView(Context context, AttributeSet attrs) {
      super(context, attrs);
      init(context);
  }
  
  private void init(Context context) {
      setOrientation(LinearLayout.VERTICAL);
      setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
      LayoutInflater inflater = LayoutInflater.from(context);
  
      //Inflate custom view into LinearLayout
      inflater.inflate(
              R.layout.merge_myview, this);
  
      //Inflate header with no parent so we can set it as the ListView header in onFinishInflate()
      mHeaderView = inflater.inflate(R.layout.header_bundles_list, null);
  }
  
  @Override
  protected void onFinishInflate() {
      super.onFinishInflate();
  
      mListView = (ExpandableListView) (findViewById(R.id.lv_expandable));
  
      Log.d(&quot;MyView&quot;, &quot;Almost done...&quot;);
  
      //Commenting out this line makes it work
      mListView.addHeaderView(mHeaderView, null, false);
  }
  }
  </code></pre>
  <p>Here's <code>merge_myview.xml</code>:</p>
  <pre><code>&lt;merge xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;ExpandableListView
          android:id=&quot;@+id/lv_expandable&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;0dp&quot;
          android:layout_weight=&quot;1&quot; /&gt;    
  &lt;/merge&gt;
  </code></pre>
  <p>Here's the header:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
       &gt;
  
      &lt;TextView
          android:id=&quot;@+id/tv_title&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_alignParentTop=&quot;true&quot;
          android:background=&quot;#EFEEFF&quot;
          android:clickable=&quot;true&quot;
          android:gravity=&quot;center&quot;
          android:minHeight=&quot;96dp&quot;
          android:onClick=&quot;openPane&quot;
          android:paddingBottom=&quot;24dp&quot;
          android:paddingTop=&quot;24dp&quot;
          android:text=&quot;HELLO!&quot;
          android:textColor=&quot;#333333&quot;
          android:textIsSelectable=&quot;true&quot;
          android:textSize=&quot;48sp&quot; /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>With a plain <code>onCreate</code> in my activity:</p>
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_myview);
  }
  </code></pre>
  <p>Where <code>activity_myview.xml</code> contains only my custom view:</p>
  <pre><code>&lt;com.example.app.MyView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:paddingLeft=&quot;@dimen/activity_horizontal_margin&quot;
      android:orientation=&quot;vertical&quot;
      android:paddingRight=&quot;@dimen/activity_horizontal_margin&quot;
      android:paddingTop=&quot;@dimen/activity_vertical_margin&quot;
      android:paddingBottom=&quot;@dimen/activity_vertical_margin&quot;/&gt;
  </code></pre>
  <p>However, when I run this app, I get the following error:</p>
  <pre><code>java.lang.NullPointerException   at
   android.widget.ListView.clearRecycledState(ListView.java:507)   at
   android.widget.ListView.resetList(ListView.java:493)   at
   android.widget.ListView.layoutChildren(ListView.java:1431)   at
   android.widget.AbsListView.onLayout(AbsListView.java:1147)   at
   android.view.View.layout(View.java:7035)   at
   android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252)   at
   android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128)   at
   android.widget.LinearLayout.onLayout(LinearLayout.java:1045)   at
   android.view.View.layout(View.java:7035)   at
   android.widget.FrameLayout.onLayout(FrameLayout.java:333)   at
   android.view.View.layout(View.java:7035)   at
   android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252)   at
   android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128)   at 
   android.widget.LinearLayout.onLayout(LinearLayout.java:1045)   at
   android.view.View.layout(View.java:7035)   at
   android.widget.LinearLayout.setChildFrame(LinearLayout.java:1252)   at
   android.widget.LinearLayout.layoutVertical(LinearLayout.java:1128)   at
   android.widget.LinearLayout.onLayout(LinearLayout.java:1045)   at
   android.view.View.layout(View.java:7035)
  </code></pre>
  <p>I suppose this is a bug that was fixed somewhere between Android 2.3.7 and 4.1.1, because I only get the error when testing with the former. <s>I can't find it in the <a href="https://code.google.com/p/android/issues/list?can=1&amp;q=expandablelistview&amp;colspec=ID+Type+Status+Owner+Summary+Stars&amp;cells=tiles" rel="nofollow noreferrer">bug tracker</a>, though. </s> So why does this error occur? And is there any workaround?</p>
  <hr />
  <h1>EDIT</h1>
  <p>I think <a href="https://code.google.com/p/android/issues/detail?id=3391&amp;can=1&amp;q=expandablelistview%20header&amp;colspec=ID%20Type%20Status%20Owner%20Summary%20Stars" rel="nofollow noreferrer">this</a> is the bug, fixed in June 2013.</p>