2

我正在尝试为我的 android 应用程序创建自定义按钮类

public class TicTacButton extends Button 

我已经在内部设置了所有构造函数TicTacButton并创建了自定义方法和属性。在我的主要活动中,我尝试将按钮初始化为

TicTacButton btn = (TicTacButton) findViewById(R.id.button1);

我得到一个

java.castClassException。android.widget.Button 不能转换为 com.example.tictactoetitan.TicTacButton

我尝试将我的 xml 文件更改为

<TicTacButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1"
        android:layout_toRightOf="@+id/button1" />

它没有用。

4

1 回答 1

4

Using the full package name in the XML file fixed it.

<com.example.tictactoetitan.TicTacButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1"
        android:layout_toRightOf="@+id/button1" />
于 2013-04-22T21:43:08.200 回答