title: Android xml merge layout error on inflate
body: <p>I have a <code>CustomView</code> class and I want to use xml layout for it. So, my class extends <code>RelativeLayout</code>, inflates xml layout and tries to attach it to self. </p>
  
  <pre><code>public class CustomView extends RelativeLayout
  {
    public CustomView (Context context)
    {
       super(context);
       LayoutInflater.from(context).inflate(R.layout.layers_list_item, this, true);
    }
  }
  </code></pre>
  
  <p>If my xml layout has some layout (Linear, for example) as root element it works fine. But when I try to use <code>&lt;merge&gt;</code> tag according to <a href="https://stackoverflow.com/a/5535518/1738427/" title="Custom View with XML Layout in Android">this response</a> I got this error: </p>
  
  <p><code>&lt;merge /&gt; can be used only with a valid ViewGroup root and attachToRoot=true</code></p>
  
  <p>My xml layout is like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;merge xmlns:android="http://schemas.android.com/apk/res/android"
       ... &gt;
  
       &lt;CheckBox
          ... /&gt;
  
       &lt;TextView
          ... /&gt;
  
  &lt;/merge&gt;
  </code></pre>
  
  <p>I also tried to remove all attributes from <code>&lt;merge... &gt;</code> tag, got the same result. What's wrong?</p>
  
  <p><strong>UPDATE</strong>: The code above is correct.
   As <em>secretlm</em> mentioned, problem was that <code>&lt;merge&gt;</code> was used as a <code>root element</code> and inflated in another piece od code:</p>
  
  <pre><code>ArrayAdapter&lt;String&gt; adapter = 
      new ArrayAdapter&lt;String&gt;(this, 
                               R.layout.layers_list_item, 
                               R.id.layers_list_item_text);
  </code></pre>
  
  <p>And with every element added adapter tried to inflate <code>R.layout.layers_list_item</code> which have <code>&lt;merge&gt;</code> as root.</p>