title: Layout not being inflated in android custom component
body: <p>I'm getting a null pointer exception in my custom view (which is derived from a <code>LinearLayout</code>) because it can't find its child views. Here is the code:</p>
  
  <pre><code>public class MyView extends LinearLayout
  {
      public MyView(Context context, AttributeSet attrs)
      {
          this(context, attrs, 0);
      }
  
      public MyView(Context context, AttributeSet attrs, int defStyle)
      {
          super(context, attrs, defStyle);
      }
  
      private TextView mText;
  
      @Override
      protected void onFinishInflate()
      {
          super.onFinishInflate();
          mText = (TextView) findViewById(R.id.text);
  
          if (isInEditMode())
          {
              mText.setText("Some example text.");
          }
      }
  }
  </code></pre>
  
  <p>Here is the layout (<code>my_view.xml</code>):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;com.example.views.MyView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal" &gt;
  
      &lt;TextView
          android:id="@+id/text"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:gravity="center"
          android:ellipsize="end"
          android:maxLines="4"
          android:paddingLeft="8dp"
          android:paddingRight="8dp"
          android:text="Some text" /&gt;
  
  &lt;/com.example.views.MyView&gt;
  </code></pre>
  
  <p>And here is how I put it in the XML file:</p>
  
  <pre><code>    &lt;com.example.views.MyView
          android:id="@+id/my_view"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" /&gt;
  </code></pre>
  
  <p>But when I try to preview it in the layout editor I get an NPE on <code>mText.setText(...)</code> because <code>getViewById()</code> returns <code>null</code>. </p>
  
  <p>What's going on?</p>
  
  <h1>Clarification</h1>
  
  <p>The reason I expect this to work, is if I do</p>
  
  <pre><code>MyView v = (MyView)inflater.inflate(R.layout.my_view);
  ((TextView)v.findViewById(R.id.text)).setText("Foo");
  </code></pre>
  
  <p>everything works fine. Is that not what the layout inflater does when it goes through a layout file? In any case, how can I handle both situations correctly (without getting pointless nested views)?</p>