title: Android: how to make findViewById(R.id.xxx) working in a class inheriting/extending from the View class?
body: <p>I have the following problem: I want to add a custom view (custom_view.xml and associated CustomView.java class) to my main activity.</p>
  
  <p>So, I do the following:</p>
  
  <p>1) In my main activity (linked to main.xml):</p>
  
  <pre><code>CustomView customView = new CustomView(this);
  mainView.addView(customView);
  </code></pre>
  
  <p>2) In my CustomView.java class (that I want to link to custom_view.xml):</p>
  
  <pre><code>public class CustomView extends View {
  
  public CustomView(Context context)
  {
  super(context);
  
  /* setContentView(R.layout.custom_view); This doesn't work here as I am in a class extending from and not from Activity */
  
  TextView aTextView = (TextView) findViewById(R.id.aTextView); // returns null
  
  ///etc....
  }
  
  }
  </code></pre>
  
  <p>My problem is that aTextView remains equal to null... It seems clearly due to the fact that my custom_view.xml is not linked to my CustomView.java class. How can I do this link ? Indeed, I tried setContentView(R.layout.custom_view); but it doesn't work (compilation error) as my class extends from View class and not Activity class.</p>
  
  <p>Thanks for your help !!</p>