Android ADT: Extending android.widget.EditText - runtime class cast exception
<p>I have a simple app which will have a bunch of EditTexts taking numeric values, and various calculations between them.  Like a unit conversion tool.</p>

<p>So I've defined CalcField as a subclass of EditText to serve as a field that contains only float values, with some extra methods, for example:</p>

<pre><code>package com.example.ex1;
import android.content.Context;
import android.widget.*;
public class CalcField extends EditText {

// no idea why this is needed.  without it i'm told an explicit constructor needed.
public CalcField(Context c) {
    super(c);
}

public void setValue(float d) {
    setText(String.valueOf(d));
}
public float getValue() {
    return Float.valueOf(getText().toString());
}
}
</code></pre>

<p>==</p>

<p>In my layout I have a field X defined like this:</p>

<pre><code>        &lt;EditText
            android:id="@+id/X"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" /&gt;
</code></pre>

<p>And in my main activity I try to grab this field from my layout, like this:</p>

<pre><code>    CalcField fieldX = (CalcField) findViewById(R.id.X);
</code></pre>

<p>Which gives a class cast exception that I don't understand:</p>

<pre><code>07-16 21:23:49.049: E/AndroidRuntime(1429): FATAL EXCEPTION: main
07-16 21:23:49.049: E/AndroidRuntime(1429): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ex1/com.example.ex1.MainActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to com.example.ex1.CalcField
07-16 21:23:49.049: E/AndroidRuntime(1429):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
</code></pre>

<p>Can anyone tell me why I can't do this simple typecast?</p>

<p>Thanks in advance</p>
