title: ClassCastException if placing Button above a view
body: <p>I have a problem with placing a Button above a separator line created in a <code>View</code>. In the following code I place buttons and text as you can see in the picture:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:paddingLeft="16dp"
      android:paddingRight="16dp" &gt;
  
      &lt;LinearLayout
          android:id="@+id/image_card_wrapper"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" &gt;
  
          &lt;ImageView
              android:id="@+id/image_card"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="16dp"
              android:baselineAlignBottom="false"
              android:contentDescription="@string/image_card"
              android:src="@drawable/image10herz" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;View
          android:id="@+id/separator_card"
          android:layout_width="fill_parent"
          android:layout_height="1dp"
          android:layout_below="@+id/image_card_wrapper"
          android:layout_marginBottom="16dp"
          android:layout_marginTop="16dp"
          android:background="@android:color/darker_gray" /&gt;
  
      &lt;TextView
          android:id="@+id/text_card_info"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/separator_card"
          android:layout_marginRight="16dp"
          android:text="test1"
          android:textSize="14sp"
          android:textStyle="bold" /&gt;
  
      &lt;View
          android:id="@+id/separator_card_info"
          android:layout_width="fill_parent"
          android:layout_height="1dp"
          android:layout_below="@+id/text_card_info"
          android:layout_marginBottom="16dp"
          android:layout_marginTop="16dp"
          android:background="@android:color/darker_gray" /&gt;
  
      &lt;Button
          android:id="@+id/button_next_card"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_below="@+id/separator_card_info"
          android:text="@string/button_next_card"
          android:textSize="12sp" /&gt;
  
      &lt;Button
          android:id="@+id/button_klo_card"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/separator_card_info"
          android:layout_toLeftOf="@+id/button_next_card"
          android:text="@string/button_klo_card"
          android:textSize="12sp"
          android:visibility="visible" /&gt;
  
      &lt;Button
          android:id="@+id/button_king"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/separator_card_info"
          android:layout_toLeftOf="@+id/button_klo_card"
          android:text="@string/button_king"
          android:textSize="12sp"
          android:visibility="visible" /&gt;
  
      &lt;ToggleButton
          android:id="@+id/button_music"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_above="@+id/separator_copyright_play"
          android:textOff="@string/button_music_off"
          android:textOn="@string/button_music_on"
          android:textSize="12sp"
          android:visibility="visible" /&gt;
  
      &lt;View
          android:id="@+id/separator_copyright_play"
          android:layout_width="fill_parent"
          android:layout_height="1dp"
          android:layout_above="@+id/text_copyright_play"
          android:layout_marginBottom="16dp"
          android:layout_marginTop="16dp"
          android:background="@android:color/darker_gray" /&gt;
  
      &lt;TextView
          android:id="@+id/text_copyright_play"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="16dp"
          android:layout_alignParentBottom="true"
          android:text="@string/text_copyright" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Here is the layout as picture:</p>
  
  <p><img src="https://i.stack.imgur.com/bJVy6.png" alt="enter image description here"></p>
  
  <p>As you can see, I want to display all buttons above this horizontal line and all button in one row with <code>android:layout_toLeftOf="..."</code>. Each time I do <code>android:layout_above="@+id/horizontal_line_bottom"</code> and the Activity is launched, I get the following <code>ClassCastException</code>:</p>
  
  <pre><code>05-28 16:48:12.624: E/AndroidRuntime(4260): FATAL EXCEPTION: main
  05-28 16:48:12.624: E/AndroidRuntime(4260): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mho.circleofdeathapp/com.mho.circleofdeathapp.PlayActivity}: java.lang.ClassCastException: android.view.View cannot be cast to android.widget.ToggleButton
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.os.Handler.dispatchMessage(Handler.java:99)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.os.Looper.loop(Looper.java:137)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread.main(ActivityThread.java:5041)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at java.lang.reflect.Method.invoke(Method.java:511)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at dalvik.system.NativeStart.main(Native Method)
  05-28 16:48:12.624: E/AndroidRuntime(4260): Caused by: java.lang.ClassCastException: android.view.View cannot be cast to android.widget.ToggleButton
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at com.mho.circleofdeathapp.PlayActivity.onCreate(PlayActivity.java:69)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.Activity.performCreate(Activity.java:5104)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
  05-28 16:48:12.624: E/AndroidRuntime(4260):     ... 11 more
  </code></pre>
  
  <p>In my <code>onCreate()</code> method I only cast the Views by ID ot the right buttons (<code>ToggleButton</code> and <code>Button</code>). This is all working fine if I don't change the settings in the layout XML, but if I do this, I get the corresponding exception if I start the <code>Activity</code>.</p>
  
  <p>EDIT: Added onCreate() method.</p>
  
  <pre><code> @Override
    protected void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_play);
      // Show the Up button in the action bar.
      setupActionBar();
  
      nextCardButton = (Button) findViewById(R.id.button_next_card);
      nextCardButton.setOnClickListener(this);
  
      kloCardButton = (Button) findViewById(R.id.button_klo_card);
      kloCardButton.setOnClickListener(this);
  
      kingButton = (Button) findViewById(R.id.button_king);
      kingButton.setOnClickListener(this);
  
      soundButton = (ToggleButton) findViewById(R.id.button_music);
      soundButton.setOnClickListener(this);
  
      imageView = (ImageView) findViewById(R.id.image_card);
      imageView.setImageBitmap(getImage(R.drawable.bier_head));
  
      cardTextView = (TextView) findViewById(R.id.text_card_info);
  
      initialize();
    }
  </code></pre>