I might be misunderstanding type casting, but here is my problem.
I’ve got an Android View with a ToggleButton:
<LinearLayout
... >
<ToggleButton
android:id="@+id/btnRec"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:textOff="Start TEST"
android:textOn="Stop TEST" />
...
</LinearLayout>
Then in my activity I declare a ToggleButton variable:
private ToggleButton mRecordButton = null;
Now, I implement a subclass of ToggleButton, called RecordButton:
class RecordButton extends ToggleButton {
OnCheckedChangeListener clicker = new OnCheckedChangeListener() {
...
};
public RecordButton(Context ctx) {
super(ctx);
setOnCheckedChangeListener(clicker);
}
}
And finally in onCreate I find the button by its id:
mRecordButton = (RecordButton) findViewById(R.id.btnRec);
This throws a ClassCastException.
Why? How can I cast it to RecordButton (the subclass) from ToggleButton (the superclass)?
in your XML, replace the regular
ToogleButtonwith the custom one..