【问题标题】:Using Material Button gives ClassNotFound runtime error使用 Material Button 会导致 ClassNotFound 运行时错误
【发布时间】:2019-02-06 06:17:33
【问题描述】:

我正在使用 XML 文件中的 <com.google.android.material.button.MaterialButton 和 Java 片段文件中的 private MaterialButton dateButton; 将我的应用程序中的按钮替换为 Material Buttons。 我研究了代码实验室“MDC-101 Android: Material Components (MDC) Basics (Java)”来了解如何使用材质按钮。在 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)

这是我的布局文件中的一个 sn-p:

<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 的 sn-p:

@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,这给了我错误。使用普通按钮作为 dateButton 时,应用程序运行良好。

这是我的 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 依赖项运行代码实验室,因此决定保留此版本以尽量减少更改。

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

【问题讨论】:

  • 貌似是multiDex错误,看看:developer.android.com/studio/build/multidex
  • 谢谢,但没有雪茄。将 minSdkVersion 更改为 21 并启用了 multiDex,但错误仍然存​​在。我认为在编译过程中不知何故不包含 Material Button 的库,但我不知道如何解决这个问题
  • 请您尝试使用 27 API。
  • 不需要。正确答案如下

标签: android material-design


【解决方案1】:

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

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

或者,如果您已经使用 google 支持设计库,则必须更改您的应用主题以从 Material Components 主题继承

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

如果您无法将主题更改为从 Material Components 主题继承,您可以从 Material Components Bridge 主题继承。

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

Reyske 是怎么说的:

请注意,它现在已经过测试版了!所以你可以使用实现'com.google.android.material:material:1.0.0'

【讨论】:

  • 感谢您为我指明正确的方向。我使用了材料页面中的小部件定义,但包含了来自 CodeLab 的依赖项。它们不混合。由于我有很多支持元素,我不得不将 XML 文件中的小部件更改为“android.support.design.button.MaterialButton”,从而解决了这个错误并产生了另一个错误:)。
  • @KvdLingen 我建议您使用经典按钮,在某些情况下使用 appCompact 按钮,并将样式/主题设置为材料,这是一个非常好的示例:stackoverflow.com/questions/26346727/…
  • 我将使用 Material 按钮,因为它们很容易被四舍五入。还是解决了这个错误。
  • 请注意,它现在已经过测试版了!所以你可以使用implementation 'com.google.android.material:material:1.0.0'
  • ClassNotFoundException 真的可以来自错误的主题定义吗?我与com.google.android.material.slider.Slider 有类似的问题(其他材料组件工作正常)(编辑:材料版本是问题)
【解决方案2】:

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

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

【讨论】:

  • 这应该是正确的答案。干得好先生!我喜欢这种新样式,当您将其复古安装到具有此要求的现有应用程序中时,它使事情变得非常容易,而不会破坏所有其他应用程序样式
  • 是的,成功了!不知道为什么,但其他 MaterialButtons 在没有 android:theme 属性的情况下工作......
  • 这个答案没有错,但是如果必须放多个MaterialButtons,那么在每个块中都需要写textAppearance。而不是这个你可以做This
【解决方案3】:

崩溃时向 Logcat 输出以下消息。

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

因此,你也可以通过添加TextAppearance来显示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" />

【讨论】:

    【解决方案4】:

    如果你在你的应用中使用了材质主题组件,那么试试这个。

    在您的 styles.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>
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        您应该使用textAppearance 和您的字体样式,而不是fontFamily 和textSize

        【讨论】:

          猜你喜欢
          • 2019-05-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-08
          • 1970-01-01
          • 2020-06-09
          相关资源
          最近更新 更多