使用Material Button会导致ClassNotFound运行时错误。

5 浏览
0 Comments

使用Material Button会导致ClassNotFound运行时错误。

我正在用Material Buttons替换我的应用中的按钮,使用XML文件中的和Java片段文件中的

我学习了代码实验室"MDC-101 Android: Material Components (MDC) Basics (Java)",了解如何使用Material Button。我在gradle.build(Module: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)

这是我的布局文件的片段:

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: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" />

android:id="@+id/btn_week"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:layout_weight="1"

/>

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" />

这是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);

我只是将dateButton更改为Material Button,这给我带来了错误。当使用普通的Button时,应用程序运行正常。

这是我的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.supportVersion是28.0.0-beta01。我知道rc-01和rc02的一些库可用,但我使用了beta01的依赖项运行了代码实验室,所以决定保持这个版本以减少更改。

有人知道是什么原因导致运行时错误吗?

0