我一直在使用扩展的自定义类(“AutoResizeTextView”) (请参阅此问题TextView
中接受的答案)。但是,当尝试初始化此 AutoResizeTextView 时,我得到一个classcastexception.
这是一些代码:
飞溅.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
...
// Line that causes error ------v
final AutoResizeTextView appName = (AutoResizeTextView) findViewById(R.id.title_text);
...
}
飞溅.xml:
<TextView
android:id="@+id/title_text"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_below="@+id/welcome_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="@string/app_name"
android:textColor="@android:color/white"
android:textSize="35dp" />
日志猫:
08-01 10:43:33.471: E/AndroidRuntime(6433): FATAL EXCEPTION: main
08-01 10:43:33.471: E/AndroidRuntime(6433): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.projects.package.name.Splash}: java.lang.ClassCastException: android.widget.TextView
08-01 10:43:33.471: E/AndroidRuntime(6433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-01 10:43:33.471: E/AndroidRuntime(6433): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-01 10:43:33.471: E/AndroidRuntime(6433): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-01 10:43:33.471: E/AndroidRuntime(6433): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-01 10:43:33.471: E/AndroidRuntime(6433): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 10:43:33.471: E/AndroidRuntime(6433): at android.os.Looper.loop(Looper.java:123)
08-01 10:43:33.471: E/AndroidRuntime(6433): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-01 10:43:33.471: E/AndroidRuntime(6433): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 10:43:33.471: E/AndroidRuntime(6433): at java.lang.reflect.Method.invoke(Method.java:521)
08-01 10:43:33.471: E/AndroidRuntime(6433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-01 10:43:33.471: E/AndroidRuntime(6433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-01 10:43:33.471: E/AndroidRuntime(6433): at dalvik.system.NativeStart.main(Native Method)
08-01 10:43:33.471: E/AndroidRuntime(6433): Caused by: java.lang.ClassCastException: android.widget.TextView
08-01 10:43:33.471: E/AndroidRuntime(6433): at my.projects.package.name.Splash.onCreate(Splash.java:44)
08-01 10:43:33.471: E/AndroidRuntime(6433): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-01 10:43:33.471: E/AndroidRuntime(6433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
我假设由于这个自定义类 extends TextView
,并且因为R.id.title_text
是 a TextView
,所以这会起作用。任何想法出了什么问题?
我尝试过的一些事情:
- 项目 -> 清理
- 重启 Eclipse
- 确保每个文件都存在于代码试图找到它的位置