问题
I have a custom TextView looking like this:
public class ClickTextView extends TextView {
public ClickTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
// and so on...
}
Why is the code below giving me a ClassCastException?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// CLASS CAST EXCEPTION HERE, this is line 69
ClickTextView tvClicks = (ClickTextView) findViewById(R.id.tvClicks);
}
And below is my main.xml, where I declare the ClickTextView with the packagename. main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.xxmassdeveloper.click.gui.ClickTextView
android:id="@+id/tvClicks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:gravity="center"
android:text="1,000,000,000"
android:textSize="45dp" />
</RelativeLayout>
Here the error log:
04-07 19:52:44.536: E/AndroidRuntime(15589): FATAL EXCEPTION: main
04-07 19:52:44.536: E/AndroidRuntime(15589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxmassdeveloper.click/com.xxmassdeveloper.click.ClickMain}: java.lang.ClassCastException: android.widget.TextView cannot be cast to com.xxmassdeveloper.click.gui.ClickTextView
04-07 19:52:44.536: E/AndroidRuntime(15589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
04-07 19:52:44.536: E/AndroidRuntime(15589): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
04-07 19:52:44.536: E/AndroidRuntime(15589): at android.app.ActivityThread.access$600(ActivityThread.java:142)
04-07 19:52:44.536: E/AndroidRuntime(15589): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
04-07 19:52:44.536: E/AndroidRuntime(15589): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 19:52:44.536: E/AndroidRuntime(15589): at android.os.Looper.loop(Looper.java:137)
04-07 19:52:44.536: E/AndroidRuntime(15589): at android.app.ActivityThread.main(ActivityThread.java:4931)
04-07 19:52:44.536: E/AndroidRuntime(15589): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 19:52:44.536: E/AndroidRuntime(15589): at java.lang.reflect.Method.invoke(Method.java:511)
04-07 19:52:44.536: E/AndroidRuntime(15589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-07 19:52:44.536: E/AndroidRuntime(15589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-07 19:52:44.536: E/AndroidRuntime(15589): at dalvik.system.NativeStart.main(Native Method)
04-07 19:52:44.536: E/AndroidRuntime(15589): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to com.xxmassdeveloper.click.gui.ClickTextView
04-07 19:52:44.536: E/AndroidRuntime(15589): at com.xxmassdeveloper.click.ClickMain.onCreate(ClickMain.java:69)
04-07 19:52:44.536: E/AndroidRuntime(15589): at android.app.Activity.performCreate(Activity.java:5008)
04-07 19:52:44.536: E/AndroidRuntime(15589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-07 19:52:44.536: E/AndroidRuntime(15589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
04-07 19:52:44.536: E/AndroidRuntime(15589): ... 11 more
I already did restart eclipse, and stuff like project-clean. I also completely deleted the app on the device and reinstalled it.
回答1:
Solved, I made a stupid misstake in my .xml file causing the Exception.
来源:https://stackoverflow.com/questions/15865688/classcastexception-when-initializing-customview