我有一个自定义TextView,如下所示:
public class ClickTextView extends TextView {
public ClickTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
// and so on...
}
为什么下面的代码会给我一个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);
}
下面是我的main.xml,我用包名声明了ClickTextView。 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>
这里是错误日志:
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
我已经重新启动了eclipse,以及类似项目清理的东西。 我还完全删除了设备上的应用程序并重新安装了它。
答案 0 :(得分:0)
解决,我在我的.xml文件中犯了一个愚蠢的错误,导致异常。