我遇到了針對android開發的早期問題。我製作了自己的自定義視圖(效果很好)。在開始時我只是以編程方式將它添加到佈局,但我想我可以嘗試將它放入XML佈局(爲了一致性)。當鑄造自定義視圖子類時發生ClassCastException
所以我得到的是:
的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(活動類):
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顯然更多,但你會得到點)
現在,這是堆棧跟蹤:
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
爲什麼不能我投我的自定義視圖?我需要它是那種類型,因爲它有一些額外的方法,我想訪問。我是否應該重構它,讓另一個類處理邏輯,然後讓視圖成爲視圖?我真的很喜歡這個工作。
感謝您的任何幫助。
您確定您的自定義組件是唯一使用該ID的組件嗎?我做了類似的事情,它適用於我... – beetstra 2010-05-23 15:05:28