当android:theme被应用于特定按钮时应用程序崩溃

Fri*_*iek 5 android themes

我正在为Android(我的第一个应用程序)做一个简单的计算器应用程序,当我尝试将特定的android:主题应用于按钮时,我遇到了一些问题.

当具有特定主题的按钮尝试在onclick事件中执行活动方法时,问题就出现了.根据我在StackOverflow中搜索的内容,就像按钮的"上下文"一样,特定主题与活动上下文不同,因此它无法找到我的方法来处理活动中写入的onclick.

有我的style.xml,我定义了我的应用程序主题和我的特定按钮主题:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="colorButtonNormal">#dc000000</item>
        <item name="android:background">#dc262626</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="contextButtonTheme" parent="AppTheme">
        <item name="colorButtonNormal">@color/contextButtonsColor</item>
    </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

布局xml中有我的按钮:

<Button
    android:layout_width="0dp"
    android:layout_weight="25"
    android:layout_height="match_parent"
    android:text="X"
    android:id="@+id/multButton"
    android:textSize="11pt"
    android:theme="@style/contextButtonTheme"
    android:onClick="onClickButton"/>
Run Code Online (Sandbox Code Playgroud)

我在这里读到的解决方案是将"android:theme"更改为"style",虽然这解决了崩溃,但colorButtonNormal新颜色并未应用:(.

请帮助我D:

PD:抱歉我的英语不好

ci_*_*ci_ 3

我在这里回答了类似的问题,您可以在其中获得更多背景信息。

解决您的问题的一个可能的解决方案是不使用android:onClick="onClickButton"而是在代码中设置 an onClickListener。这样你就可以保持你的主题Button。官方文档有一个如何执行此操作的示例。