安卓错误: Can't find ColorStateList from drawable resource while using dialogs

标签 android kotlin dialog material-design datepickerdialog

我正在尝试对新的 Android Material 库进行测试,但经过数小时的研究后,我无法弄清楚如何解决此问题。

当我尝试打开任何类型的对话框时,都会发生错误。

这是错误

android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID #0x7f070018 at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:1042) at android.content.res.Resources.loadColorStateList(Resources.java:1041) at android.content.res.TypedArray.getColor(TypedArray.java:469) at com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:2436) at com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2672) at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:410) at com.android.internal.app.AlertController.installContent(AlertController.java:263) at android.app.AlertDialog.onCreate(AlertDialog.java:436) at android.app.Dialog.dispatchOnCreate(Dialog.java:407) at android.app.Dialog.show(Dialog.java:302)

ID 为 #0x7f070018 的资源解析为 abc_dialog_material_background,它是 appcompat 库的一部分。

这是调用错误的代码

val c = Calendar.getInstance()
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH)
        val day = c.get(Calendar.DAY_OF_MONTH)

        DatePickerDialog(requireContext(), DatePickerDialog.OnDateSetListener { _, y, m, dayOfMonth ->
            println(y)
            println(m)
            println(dayOfMonth)
        }, year, month, day).show()

这是我的模块 build.gradle 文件

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs'

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion '28.0.2'

    dataBinding {
        enabled = true
    }

    defaultConfig {
        applicationId 'moe.anekoisfinetoo.peek'
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode 1000
        versionName '0.1.0'
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    /* Any jar files */
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    /* Android Constraint Layout
     * https://developer.android.com/training/constraint-layout/index.html */
    implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"

    /* Android KTX
     * https://developer.android.com/kotlin/ktx */
    implementation "androidx.core:core-ktx:$rootProject.ktxVersion"

    /* Android Lifecycle Libraries
     * https://developer.android.com/topic/libraries/architecture/lifecycle */
    implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.lifecycleExtensionsVersion"

    /* Android Material Components
     * https://github.com/material-components/material-components-android */
    implementation "com.google.android.material:material:$rootProject.materialVersion"

    /* Android Navigation
     * https://developer.android.com/topic/libraries/architecture/navigation */
    implementation "android.arch.navigation:navigation-fragment-ktx:$rootProject.navigationVersion"
    implementation "android.arch.navigation:navigation-ui-ktx:$rootProject.navigationVersion"
    androidTestImplementation "android.arch.navigation:navigation-testing-ktx:$rootProject.navigationVersion"

    /* Android Support Libraries
     * https://developer.android.com/topic/libraries/support-library/index.html */
    implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"

    /* Kotlin
     * https://kotlinlang.org/ */
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$rootProject.kotlinVersion"

    /* Material Dialogs
     * https://github.com/afollestad/material-dialogs */
    implementation "com.afollestad.material-dialogs:core:$rootProject.materialDialogs"

    /* Testing */
    testImplementation "junit:junit:$rootProject.junitVersion"
    androidTestImplementation "androidx.test:runner:$rootProject.runnerVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.espressoCoreVersion"
}

所有版本均从项目 build.gradle 中检索,并且都是最新的。

最佳答案

在一个新项目中一步步重新做完所有操作后发现问题,它是应用程序主题中的一个项目。

<item name="android:statusBarColor">?android:attr/windowBackground</item>

删除该行或将值更改为 colors.xml 中定义的颜色后,对话框再次开始工作。因为我希望状态栏具有与背景相同的颜色,所以我只是为此进行了更改,获得了相同的结果和工作对话框。

<item name="android:statusBarColor">?android:attr/colorBackground</item>

关于安卓错误: Can't find ColorStateList from drawable resource while using dialogs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52131284/

相关文章:

jQuery UI 对话框 - 在对话框中打开外部动态 php 文件

java - NullPointerException:DialogAlert 中的 EditText

winapi - win32 对话框应用程序 : how to make the dialog box hidden on startup?

android - MOV 指令上的 ARM SIGSEGV

java - 检测单元类测试 - 无法在未调用 Looper.prepare() 的线程内创建处理程序

android - 如何在 ScrollView 中将 LinearLayout 高度设置为匹配父级

java - 三星 J5 中的 Android Textview settext 问题

android - 协程在取消时注销接收者

android - 如何使用导航组件向变化的 fragment 添加动画?

android - Kotlin 泛型 - 调用伴随对象方法