- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我最近将我的应用程序移植到 Material Design,我开始收到奇怪的错误,提示在 Activity 中找不到方法(虽然它们在过去 3 年左右的时间里存在)并且一切都以用户崩溃告终。这似乎只发生在运行 Android 4.*(4.0.2、4.2、4.4)、各种设备(三星和 LG)的用户身上,如有任何解决方法的建议,我们将不胜感激! :)
完整错误信息:
Fatal Exception: java.lang.IllegalStateException Could not find a method onCancelClicked(View) in the activity class androidx.appcompat.widget.TintContextWrapper for onClick handler on view class com.google.android.material.button.MaterialButton with id 'btnCancel'
(...)
Caused by java.lang.NoSuchMethodException onCancelClicked [class android.view.View]
斜体部分为自定义方法,布局见下
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.TextButton"
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onCancelClicked"
android:text="@string/dialog_conf_code_cancel" />
处理此布局的 Activity 实现 AppCompatActivity
,而 onCancelClicked
很简单,如下所示:
public void onCancelClicked(View v) {
this.finish();
}
对于以这种方式实现的其他按钮,我也遇到了类似的错误,再次 - 到目前为止仅适用于使用 Android 4.* 的一些用户。
我在 build.gradle
中的依赖定义如下
dependencies {
implementation 'com.google.android.material:material:1.0.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'com.google.android.gms:play-services-ads:16.0.0'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.6'
implementation 'com.android.support:appcompat-v7:28.2.0'
implementation 'com.android.support:design:28.2.0'
}
再一次 - 任何建议表示赞赏! :)
最佳答案
在检查了几个看似相关的链接后,最好的做法是从 XML 布局中删除所有 android:onClick
并改为在 Java 中附加点击监听器。
我对此的感觉是,尚不清楚 Android 和 AndroidX 的哪些组合会导致此问题。所以在 Java 中设置监听器似乎是最好的解决方案,因为它完全避免了这个问题。
另见:
关于android - 无法在 androidx.appcompat.widget.TintContextWrapper 中为 MaterialButton 中的 onClick 处理程序找到自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54028752/
我在布局中有四个 MaterialButton,我想在按下其中一个按钮时更改所有按钮的颜色。其中两个带有一种颜色,另外两个带有另一种颜色。我该怎么做? 最佳答案 只需调用 setState() 并使用
安卓工作室 3.3.2 classpath 'com.android.tools.build:gradle:3.3.2' 在我的新 Android 项目中,我想使用新的 android Materia
我正在尝试使用 MaterialButton,但它无法正常工作。我的 androidx 依赖 implementation 'androidx.appcompat:appcompat:1.0.2' i
我正在尝试创建一个从 MaterialButton 扩展的自定义 View 并在代码中应用样式,因此我不需要在 xml 中执行此操作。 class CustomRedButton @JvmOverlo
我想以编程方式创建一个按钮,如此处的设计指南中所定义:https://material.io/design/components/buttons.html#outlined-button ,看起来像这
我正在这样做 但是图标显示在文本的左侧,图标重力对图标位置没有影响。 在普通按钮中,我们曾经使用 drawableRight 实现相同的效果,但这里没有找到方法。 最佳答案 app:icon="@d
我想将图标放在 materialButton 的顶部,如下所示: 但是我在 MaterialButton 中找不到任何属性来设置应用程序的重力:图标到顶部。我只能将它设置为开始或结束。有没有什么方法可
我正在尝试使用一个 Material 按钮,我想要在最左边有一个图标,然后在中间有一些文本。但是,例如,当我将图标放在左侧时,我可以清楚地看到文本被推到右侧。有什么办法可以避免这种情况吗?我希望文本居
我正在使用最新版本的 com.google.android.material:material 库(即 1.1.0-alpha03),并且我有一个 MaterialButton 使用图标定义,无文本,
这是按钮的代码 这是尝试在按钮中添加描边时的图像 Image With added stroke in button 下面是没有笔画宽度的代码 Image without Stroke 我尝试了不
您好,我遇到了在 Android 上对齐 Material 按钮中的图标的问题。我有 match_parent 宽度、文本和文本旁边的图标的按钮。我想将图标 8dp 左对齐到文本,我尝试通过 com.
安卓6.0 安卓工作室 3.6 在我的 fragment 中,当单击按钮时,我会像这样“即时”更改样式: bluetoothPageViewModel.isDisableModeLiveData(
参见: 代码: 样式.xml rounded 20dp 为了确认 Material 按钮包含完整空间,我将
我正在尝试构建一个带有边框按钮的布局,如下所示(预期行为)。从 Material design 文档中,我了解到 Outlined Material Button 似乎非常适合我的目的。我在我的布局中
我们正在使用 Material Components 1.0.0,并希望在整个应用程序中更改 MaterialButton 的样式。我们创建了自己的样式,扩展了组件库中的样式。现在我们有类似的东西:
我正在使用 supportLibrary = "28.0.0-beta01" 版本。 这是我在 .xml 文件中的代码: 到位于按钮左侧的可绘制对象的代码图标。我想将按钮设置为中心。我想达到这个结果
我正在使用支持库中的 Materialbutton。我希望图标位于文本的右侧?我似乎找不到如何去做。感谢任何帮助 最佳答案 您可以使用 app:iconGravity 属性。 使用 app:iconG
我这辈子都想不通按钮宽度的问题。我不知道按钮当前的宽度是多少,但我可以告诉你,当我告诉它时它并没有减少。它保持完全相同,并且在增加 minWidth 值时甚至不会增加。 Material( chi
我尝试向 MaterialButton 添加一个简单的 BadgeDrawable,但没有显示徽章。 我已经在我的 build.gradle 中尝试了以下版本: 'com.google.android
如果我设置 Theme.MaterialComponents.Light 作为我的主题,如果我在 xml 布局中使用这两个按钮,它们之间会有什么区别吗? 正如我所看到的,它们都表现为 Materi
我是一名优秀的程序员,十分优秀!