使用自定义TextView类时的Classcastexception

时间:2012-08-01 15:02:01

标签: android textview classcastexception

我一直在使用扩展TextView的自定义类(“AutoResizeTextView”)(请参阅this question中接受的答案)。但是,在尝试初始化此AutoResizeTextView时,我得到classcastexception.

以下是一些代码:

Splash.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);

    ...
}

splash.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" />

logcat的:

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)

我认为,由于此自定义类扩展了TextView,并且由于R.id.title_textTextView,因此可以使用。任何想法出了什么问题?

我尝试过的一些事情:

  • 项目 - &gt;清洁
  • 重启Eclipse
  • 确保每个文件都存在于代码试图在
  • 找到它的位置

2 个答案:

答案 0 :(得分:3)

这是你的问题:

<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" />

而不是<TextView,您应该以这种方式指明自定义TextView

<your.package.AutoResizeTextView 
        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" />

答案 1 :(得分:1)

TextView的自定义AutoResizeTextView 只需在xml中稍微改变一下

<yourpackagename.AutoResizeTextView
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    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" />