title: Combine XML RelativeLayout with Surface View
body: <p>I'm new to Android app development and I would like to ask something.</p>
  
  <p>I have a bitmap inside my SurfaceView that I want to animate and move programmatically. I also want to put a button in the screen to control the moving of the bitmap. But since I cannot put a button in a SurfaceView, I declared it inside a RelativeLayout in my XML layout called main.xml.</p>
  
  <p>Here's what's inside my main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@+id/main_layout" &gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:text="Test Button" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>So what I'm trying to do is to "combine" (can't find a better word) the SurfaceView with the Layout that is in my main.xml.</p>
  
  <p>Here is my Main.java where I try to combine the Layout w/ the SurfaceView</p>
  
  <pre><code>package com.src.test.view;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.Button;
  import android.widget.RelativeLayout;
  
  public class Main extends Activity {
  
      private RelativeLayout _myXmlLayout;
      private MyView _myView;
      private Button _myButton;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          _myXmlLayout = (RelativeLayout) findViewById(R.id.main_layout);
          _myView = new MyView(this);
          _myButton = (Button) findViewById(R.id.button1);
  
          _myXmlLayout.addView(_myButton);
          _myXmlLayout.addView(_myView);
  
          setContentView(_myXmlLayout);
      }
  }
  </code></pre>
  
  <p>but it gives me an "application has stopped unexpectedly" error. Am I doing it wrong? If so, can you suggest a better way of implementing this?</p>
  
  <p>Your response is highly appreciated. Thanks!</p>
  
  <p>p.s. no bitmap animation codes and button controls yet. It's just the views that I'm currently solving. :)</p>
  
  <p>EDIT:</p>
  
  <p>I edited my codes and here it is:</p>
  
  <p>so here's my edited Main.java</p>
  
  <pre><code>package com.src.test.view;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.Window;
  
  public class Main extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.main);
  
      }
  }
  </code></pre>
  
  <p>and here's my main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" &gt;
  
      &lt;com.src.test.view.MyView
          android:id="@+id/myView" 
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" /&gt;
  
      &lt;Button
          android:text="Test Button"
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="left" /&gt;
      &lt;Button
          android:text="Test Button2"
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="right" /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>I tried to run it but it gives me an error.</p>
  
  <p>Here's the logcat</p>
  
  <pre><code>(ZygoteInit.java:626)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at dalvik.system.NativeStart.main(Native Method)
  10-29 01:18:51.046: E/AndroidRuntime(318): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.src.test.view.MyView
  10-29 01:18:51.046: E/AndroidRuntime(318):  at android.view.LayoutInflater.createView(LayoutInflater.java:503)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at android.app.Activity.setContentView(Activity.java:1647)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at com.src.test.view.Main.onCreate(Main.java:12)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
  10-29 01:18:51.046: E/AndroidRuntime(318):  ... 11 more
  10-29 01:18:51.046: E/AndroidRuntime(318): Caused by: java.lang.NoSuchMethodException: MyView(Context,AttributeSet)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at java.lang.Class.getMatchingConstructor(Class.java:660)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at java.lang.Class.getConstructor(Class.java:477)
  10-29 01:18:51.046: E/AndroidRuntime(318):  at android.view.LayoutInflater.createView(LayoutInflater.java:475)
  10-29 01:18:51.046: E/AndroidRuntime(318):  ... 21 more
  </code></pre>
  
  <p>Thank you for your response</p>