Android错误:使用对话框时无法从可绘制资源中找到ColorStateList

时间:2018-09-01 19:03:11

标签: android kotlin dialog material-design datepickerdialog

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

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

这是错误

  

android.content.res.Resources $ NotFoundException:找不到   可绘制资源ID#0x7f070018中的ColorStateList           在android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:1042)           在android.content.res.Resources.loadColorStateList(Resources.java:1041)           在android.content.res.TypedArray.getColor(TypedArray.java:469)           在com.android.internal.policy.PhoneWindow.generateLayout(PhoneWindow.java:2436)           在com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2672)           在com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:410)           在com.android.internal.app.AlertController.installContent(AlertController.java:263)           在android.app.AlertDialog.onCreate(AlertDialog.java:436)           在android.app.Dialog.dispatchOnCreate(Dialog.java:407)           在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项目中获取并为最新版本。

1 个答案:

答案 0 :(得分:1)

在一个新项目中逐步完成所有步骤后发现了问题,这是应用程序主题中的一个项目。

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

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

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