Android – using the material button will result in a classnotfound runtime error
I am replacing buttons and material buttons in my application with < com.google.android.material.button.materialbutton and private materialbutton datebutton in the XML file; In the Java fragment file, I studied the code lab "mdc-101 Android: material components (MDC) Basics (Java)" to see how to use the material button. In the gradle.build (module: APP) file, I added dependencies in the code lab. the code lab compiled and ran well. My application compiled normally, but there was an error when expanding the fragment layout:
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)
This is a fragment of my layout file:
<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>
This is a fragment of 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);
I only changed the datebutton to material button, which gave me an error. When using the daily button as the datebutton, the application works normally
This is my gradle.build file:
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'
Among them, $rootproject.supportversion is 28.0.0-beta01. I know rc-01 and rc02 of some libraries are available, but I ran the code lab with beta01 dependency, so I decided to keep this version to minimize changes
Who knows what causes runtime errors?
resolvent:
Because it suggests here that you must add a dependency in build.gradle:
implementation 'com.google.android.material:material:1.0.0-beta01'
Or, if you already use the Google supported design library, you must change your application theme to inherit the material components theme
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
<!-- ... -->
If you cannot change the theme to inherit from the material components theme, you can inherit from the material components bridge theme
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
<!-- ... -->
What does Resker say:
Please note that it is out of date now! Therefore, you can use 'com. Google. Android. Material: Material: 1.0.0'