使用材质按钮会给出 ClassNotFound 运行时错误



我正在使用XML文件中的<com.google.android.material.button.MaterialButton和Java片段文件中的private MaterialButton dateButton;将应用程序中的按钮替换为材质按钮。 我研究了代码实验室"MDC-101 Android:Material Components (MDC( Basics (Java(",以了解如何使用Material Button。在gradle.build(模块:app(文件中,我添加了依赖项,就像在代码实验室中一样。 代码实验室编译并运行良好。我的应用程序编译良好,但在膨胀片段布局时出错:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: nl.kl_solutions.schedulecompareforzermelo, PID: 16708
android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class com.google.android.material.button.MaterialButton
Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class com.google.android.material.button.MaterialButton
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.material.button.MaterialButton" on path: DexPathList[[zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/base.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_dependencies_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_resources_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_0_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_1_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_2_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_3_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_4_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_5_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_6_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_7_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_8_apk.apk", zip file "/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/nl.kl_solutions.schedulecompareforzermelo-o6fuvqQPirym08EhaTRI6Q==/lib/x86_64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)

这是我的布局文件中的一个片段:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/primaryLightColor"
android:paddingLeft="@dimen/rasterleftpadding"
android:paddingRight="@dimen/rasterrightpadding" >
<Button
android:id="@+id/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:text="prev" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_week"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
/>
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="next" />
</LinearLayout>

这是来自onCreateView的片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragmentLayout = inflater.inflate(R.layout.fragment_week_schedule, container, false);
//get references to the buttonBar buttons.
leftButton = fragmentLayout.findViewById(R.id.btn_previous);
rightButton = fragmentLayout.findViewById(R.id.btn_next);
dateButton = fragmentLayout.findViewById(R.id.btn_week);

我只将日期按钮更改为材料按钮,这给了我错误。当使用普通按钮作为日期按钮时,该应用程序运行良好。

这是我的gradle.build文件:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "com.android.support:appcompat-v7:$rootProject.supportVersion"
implementation "com.android.support:preference-v7:$rootProject.supportVersion"
implementation "com.android.support:recyclerview-v7:$rootProject.supportVersion"
implementation "com.android.support:cardview-v7:$rootProject.supportVersion"
implementation "com.android.support.constraint:constraint-layout:1.1.3"
implementation "com.android.support:design:$rootProject.supportVersion"
implementation "com.android.support:support-v4:$rootProject.supportVersion"
implementation 'com.google.code.gson:gson:2.8.5'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//database components
implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
// Lifecycle components
implementation "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"
//QR library
implementation 'me.dm7.barcodescanner:zxing:1.9.8'

其中$rootProject.support版本是28.0.0-beta01。我知道某些库的 rc-01 和 rc02 可用,但我使用 beta01 依赖项运行代码实验室,因此决定保留此版本以尽量减少更改。

有人知道导致运行时错误的原因吗?

正如它建议的那样,你必须在你的build.gradle中添加一个依赖项:

implementation 'com.google.android.material:material:1.0.0-beta01'

或者,如果您已经使用 Google 支持设计库,则必须更改应用程序主题以继承材质组件主题

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
<!-- ... -->

如果无法将主题更改为从材质组件主题继承,则可以从材质组件桥主题继承。

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
<!-- ... -->

雷斯克怎么说:

请注意,它现在已经退出测试版!因此,您可以使用实现'com.google.android.material:material:1.0.0'

在我看来,最好的选择是为材质按钮创建一个单独的自己的样式,它扩展了材质主题:

<style name="MatButton" parent="Theme.MaterialComponents">
<item name="colorOnPrimary">@android:color/holo_red_light</item>
<item name="colorPrimary">@android:color/holo_orange_dark</item>
<item name="colorOnSurface">@android:color/holo_orange_light</item>
</style>
<com.google.android.material.button.MaterialButton
android:id="@+id/searchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:textColor="@android:color/white"
android:textAppearance="@style/TextAppearance.MaterialComponents.Button"
android:theme="@style/MatButton"
/>

如果您在应用程序中使用材质主题组件,请尝试此操作。

更改样式.xml从之前到之后 ->

以前:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

后:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

崩溃时 以下消息被输出到 Logcat。

原因:java.lang.IllegalArgumentException:此组件要求您指定有效的 TextAppearance 属性。更新应用主题以继承自 Theme.MaterialComponents(或后代(。

因此,您还可以通过添加 TextLook 来显示 MaterialButton。

<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/ok"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

尝试更新应用主题以继承自Theme.MaterialComponents(或后代(

你应该使用textAppearance和字体样式,而不是fontFamilytextSize

有时;如果要创建要在其他应用程序中使用的库,则无法告诉应用程序更改其主题。相反,您可以将所需的主题(android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar"(添加到本地布局文件中,如下所示:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/primaryLightColor"
android:paddingLeft="@dimen/rasterleftpadding"
android:paddingRight="@dimen/rasterrightpadding"
android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar">
...
</LinearLayout>

这应该可以解决 ClassNotFoundException。

最新更新