【问题标题】:Creating custom button class in Android在 Android 中创建自定义按钮类
【发布时间】:2013-04-22 20:16:46
【问题描述】:

我正在尝试为我的 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" />

没用。

【问题讨论】:

  • 你应该在布局中使用完整的包名your.package.TicTacButton,而不是使用TicTacButton
  • 您还必须发布 button1 的 xml。您发布的内容定义了 button2。我们如何判断您是使用 Button 标签还是 TicTacButton 标签定义了 button1?
  • 使用 XML 文件中的完整包名修复它。
  • @harism 您应该将您的评论变成答案,以便 OP 将其标记为“已接受”。

标签: java android android-emulator custom-controls


【解决方案1】:

使用 XML 文件中的完整包名修复它。

<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-12
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2011-11-06
    • 2010-12-23
    • 1970-01-01
    • 2021-11-18
    相关资源
    最近更新 更多