ClassCastException when casting custom View subclass

Posted by Jens Jacob on Stack Overflow See other posts from Stack Overflow or by Jens Jacob
Published on 2010-05-23T14:50:16Z Indexed on 2010/05/23 14:50 UTC
Read the original article Hit count: 489

Filed under:

Hi

I've run into an early problem with developing for android. I've made my own custom View (which works well). In the beginning i just added it to the layout programmatically, but i figured i could try putting it into the XML layout instead (for consistency).

So what i got is this:

main.xml: [...] <sailmeter.gui.CompassView android:id="@+id/compassview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/widget55" android:background="@color/white" /> [...]

CompassView.java:

public class CompassView extends View { }

SailMeter.java (activity class):

public class SailMeter extends Activity implements PropertyChangeListener {

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); compassview = (CompassView) findViewById(R.id.compassview1); [...] } }

(Theres obviously more, but you get the point)

Now, this is the stacktrace:

05-23 16:32:01.991: ERROR/AndroidRuntime(10742): Uncaught handler: thread main exiting due to uncaught exception  
05-23 16:32:02.051: ERROR/AndroidRuntime(10742): java.lang.RuntimeException: Unable to start activity ComponentInfo{sailmeter.gui/sailmeter.gui.SailMeter}:   java.lang.ClassCastException: android.view.View
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.os.Looper.loop(Looper.java:123)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at java.lang.reflect.Method.invoke(Method.java:521)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at dalvik.system.NativeStart.main(Native Method)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742): Caused by: java.lang.ClassCastException: android.view.View
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at sailmeter.gui.SailMeter.onCreate(SailMeter.java:51)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-23 16:32:02.051: ERROR/AndroidRuntime(10742):     ... 11 more

Why cant i cast my custom view? I need it to be that type since it has a few extra methods in it that i want to access. Should i restructure it and have another class handle the logic, and then just having the view being a view?

Thanks for any help.

© Stack Overflow or respective owner

Related posts about android