开发者

ClassCastException when casting custom View subclass

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 { 
protected void onDraw(Canvas canvas) { [...] }
public void setBearing(float bearing) { [...] }
}

SailMeter.java (activity class):

public class SailMeter extends Activity implements PropertyChangeListener {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
compassview = (CompassView) findViewById(R.id.compassview1); //Line 51 in SailmMeter.java
[...]
}
}

(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.Han开发者_C百科dler.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? I'd really like this to work though.

Thanks for any help.


I've done this before and found it necessary to use a LayoutInflater. Perhaps you could try something like this:

public CompassView( Context context, AttributeSet attributeSet ) 
{
    super( context, attributeSet );

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate( R.layout.name_of_your_compass_view_layout_file, this );
}


Cheers everyone!

I dunno what caused it, but i ended up closing eclipse, refreshing all the files and then i noticed that the xml still had it defined as a view, not a compassview. So i changed that (and fixed a few other bugs) and then it worked.

Thanks for the help!


Workaround to fix: Assign some new 'id' value for the CustomView in Layout XML and findViewById().

It is most probably a bug.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜