首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MaterialButton ClassCastException

MaterialButton ClassCastException
EN

Stack Overflow用户
提问于 2021-10-18 07:27:29
回答 2查看 95关注 0票数 0

最近,我们移动到我们的应用程序使用材料设计主题(我们使用的是1.4.0版本)。作为其中的一部分,我们将显式的<com.google.android.material.button.MaterialButton声明转换为<Button,因为Button将在幕后转换为MaterialButton。除了下面这种情况之外,其他一切都运行得很好。

我们在FrameLayout中有一个<com.google.android.material.button.MaterialButton,如下所示

代码语言:javascript
复制
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <Button
      ...
      ...
    />
</FrameLayout>

并通过视图绑定将其用作自定义视图,如下所示

代码语言:javascript
复制
class MyView: FrameLayout {

private val binding by ViewGroupBindingProperty(MyViewBinding::inflate)

    constructor(...)
    /// other code
}

当我尝试将上面的

代码语言:javascript
复制
Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to com.google.android.material.button.MaterialButton
        at jp.ne.paypay.android.app.view.paymentMethod.MyView.init(MyView.kt:51)
        at xx.xx.xx.xx.MyView.<init>(MyView.kt:29)

任何关于哪里可能出错的指导都是非常感谢的。提前谢谢你。

EN

回答 2

Stack Overflow用户

发布于 2021-10-18 08:40:33

我认为您需要在xml中将按钮指定为com.google.android.material.button.MaterialButton,而不是普通的Button,尽管在使用material样式时,这应该由布局膨胀器来完成

票数 0
EN

Stack Overflow用户

发布于 2021-10-19 02:23:35

@braj请给我你的完整代码,然后你会问为什么

<按钮声明在我的情况下不起作用

因为Button是Android SDK的一部分,所以你可以使用Material按钮材料设计的一部分,它们是相似的,但Material按钮有很多优点,你可以用Material Design编写漂亮的代码

在您的情况下,必须将视图实现为Material Button,而不仅仅是button

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69612237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档