How to get the surface view instance
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/1691569/findviewbyid-returns-null-for-custom-component-in-layout-xml-not-for-other-co">findViewById() returns null for custom component in layout XML, not for other components</a>  </p>
</blockquote>



<p>"findViewById" returns null on the following code.<br>
Is anything wrong with this code?<br>
By the way, I'm thinking to call an animation method which the surfaceview Activity has. <br>
The class "MainBackgroundView" extends SurfaceView.<br></p>

<p>layout.xml<br></p>

<pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" &gt;

...............

&lt;com.xfirm.myapps.MainBackgroundView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/surface_mainbackground"
        android:layout_width="match_parent"
        android:layout_height="match_parent" &gt;
&lt;/com.xfirm.myapps.MainBackgroundView&gt;

...............

&lt;/LinearLayout&gt;
</code></pre>

<p>MainActivity.java<br></p>

<pre><code>.............

testButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        MainBackgroundView surfaceview = (MainBackgroundView)findViewById(R.id.surface_mainbackground);
});

.............
</code></pre>
