Search code examples
androidbuttoncastingviewflipperclasscastexception

ViewFlipper is a Button?


I have recently decided to add a ViewFlipper to my application, here is what I am have done:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/steve"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    // Child elements here.

</ViewFlipper>

MyApp.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.steve);

However when I run my application I get a java.lang.ClassCastException: android.widget.Button for the line where I cast to a ViewFlipper.

Upon debugging, it turns out that findViewById(R.id.steve) returns a Button, not a ViewFlipper.

How can I fix this?


Solution

  • Clean and rebuild your project. This should solve it. And, no, ViewFlipper is not a Button or a subclass of a Button.