错误:膨胀类com.google.android.material.switchmaterial.SwitchMaterial。

20

我在一个布局中使用了SwitchMaterial

<com.google.android.material.switchmaterial.SwitchMaterial...

Firebase Crashlytics给我发了许多用户出现以下错误的警报:

致命异常:java.lang.RuntimeException 无法启动活动ComponentInfo{...}: android.view.InflateException:二进制XML文件第324行:二进制XML文件第324行:Error inflating class com.google.android.material.switchmaterial.SwitchMaterial

原因是android.content.res.Resources$NotFoundException 文件 res/drawable/abc_switch_thumb_material.xml 的可绘制资源ID为#0x7f080047。

应用程序主题:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">...

材料依赖:

implementation "com.google.android.material:material:1.6.1"

这是什么意思?

更新

我尝试使用 AndroidX AppCompat 库中的 Switch,但在 drawable 方面仍然存在相同问题:

Fatal Exception: java.lang.RuntimeException Unable to start activity ComponentInfo{****.ui.MainActivity}: android.view.InflateException: Binary XML file line #324: Binary XML file line #324: Error inflating class androidx.appcompat.widget.SwitchCompat

Caused by android.content.res.Resources$NotFoundException Drawable ***:drawable/abc_switch_thumb_material with resource ID #0x7f080047

这太奇怪了,它在我的智能手机上运行良好,但已经影响到了 22 位用户

是的,我对发行版本启用了以下两个选项

minifyEnabled true
shrinkResources true

对于不同的应用程序,我从未在proguard文件中添加任何内容来保留某些第三方库(尤其是Google库)的可绘制对象abc_switch_thumb_material,但它始终运行良好。

更新2:

我刚刚尝试从我的AAB文件中下载由Google Play控制台生成的APK

enter image description here

我检查了apk\res\drawable\并发现可绘制对象可用

enter image description here

这就是魔法...

但是此生成的APK是通用的,Google Play Consoles会根据设备类型及其Android版本生成不同的APK,因此我不知道在这种情况下会发生什么

更新3:

即使Android Studio显示以下警告,我也将尝试仅使用Switch

使用AppCompat的SwitchCompat或Material库的SwitchMaterial

但至少我希望我的应用程序不会因此而在某些用户处崩溃

附:发生错误的设备(Firebase Crashlytics):

enter image description here

enter image description here

该应用程序的build.gradle文件(如果有帮助,可能存在库之间的冲突)

项目build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext {
        kotlin_version = "1.7.10"
        nav_version = "2.5.1"
        hilt_version = "2.43.2"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.2.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.gms:google-services:4.3.13"
        classpath "com.google.firebase:firebase-crashlytics-gradle:2.9.1"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" // for DataStore

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用程序模块 build.gradle 文件:

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "kotlin-kapt"
    id "com.google.gms.google-services"
    id "com.google.firebase.crashlytics"
    id "kotlin-parcelize"
    id "dagger.hilt.android.plugin"
    id "androidx.navigation.safeargs.kotlin"
    id "kotlinx-serialization" // for DataStore
}

android {
    compileSdkVersion 32
    defaultConfig {
        applicationId ***
        minSdkVersion 21
        targetSdkVersion 32
        multiDexEnabled true
        versionCode 18
        versionName "1.0.15"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            manifestPlaceholders = [crashlyticsEnabled: false]
            signingConfig signingConfigs.release
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
            manifestPlaceholders = [crashlyticsEnabled: true]
            signingConfig signingConfigs.release
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_11
    }
    buildFeatures {
        dataBinding = true
    }
}

dependencies {
    implementation "androidx.core:core-ktx:1.8.0"
    implementation "androidx.appcompat:appcompat:1.5.0"
    implementation "androidx.activity:activity-ktx:1.5.1"
    implementation "androidx.fragment:fragment-ktx:1.5.2"

    implementation "androidx.multidex:multidex:2.0.1"

    implementation "com.google.android.material:material:1.6.1"
    implementation "androidx.constraintlayout:constraintlayout:2.1.4"
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

    implementation "com.jakewharton.timber:timber:5.0.1"

    implementation platform("com.google.firebase:firebase-bom:30.3.2")
    implementation "com.google.firebase:firebase-analytics"
    implementation "com.google.firebase:firebase-crashlytics"

    implementation "com.google.firebase:firebase-ads:21.1.0"
    implementation "com.google.android.ump:user-messaging-platform:2.0.0"

    implementation "com.android.billingclient:billing-ktx:5.0.0"

    def lifecycle_version = "2.5.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

    // Navigation Component
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

    // Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"

    // OkHttp
    implementation(platform("com.squareup.okhttp3:okhttp-bom:4.9.0"))
    implementation("com.squareup.okhttp3:okhttp")
    implementation("com.squareup.okhttp3:logging-interceptor")

    // Retrofit
    def retrofit = "2.9.0"
    implementation "com.squareup.retrofit2:retrofit:$retrofit"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit"

    // Glide
    def glide_version = "4.13.2"
    implementation "com.github.bumptech.glide:glide:$glide_version"
    kapt "com.github.bumptech.glide:compiler:$glide_version"

    // Pagination
    implementation "androidx.paging:paging-runtime-ktx:3.1.1"

    // Datastore
    implementation "androidx.datastore:datastore:1.0.0"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.4.0"

    implementation "org.jsoup:jsoup:1.15.2"

    testImplementation "junit:junit:4.13.2"
    androidTestImplementation "androidx.test.ext:junit:1.1.3"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
}

PreferenceGroupAdapter填充SwitchCompat时,我也发现了这个问题,但(到目前为止)只在运行Android 8.1的Nexus 5X上出现。我怀疑这与Google Play提供的分离APK有关,以某种方式安装在不兼容的设备上(备份恢复?第三方APK主机?)。所涉及的缺失资源(abc_btn_switch_to_on_mtrl_00012)没有默认密度版本(仅mdpihdpixhdpixxhdpixxxhdpi)。也许他们已经将高dpi设备的分离APK安装到了低设备上?无论如何,我无法重现它。 - technicalflaw
我在Android Material Library中开了一个bug:https://github.com/material-components/material-components-android/issues/3032 - Javier Delgado
4
你有没有找到解决这个问题的方法?我的用户也遇到了崩溃的情况。 - Jazib Khan
我在使用MaterialSwitch时遇到了相同的问题。 - tzegian
2个回答

6

当我开始使用<Switch>时,崩溃问题得到了解决。

<SwitchCompat><SwitchMaterial>都会在生产环境中导致崩溃。


-2
我尝试将应用程序清单中的android:theme更改为我的父活动名称,并且成功了。

根据目前的写法,你的回答不够清晰。请编辑以添加更多细节,帮助其他人理解这如何回答所提出的问题。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接