Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android error: Can't find ColorStateList from drawable resource while using dialogs

I'm trying to give a test to the new Android Material libraries but after hours and hours of investigate I can't figure out how to fix this problem.

The error happens when I try to open any kind of dialog.

This is the error

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)

The resource with ID #0x7f070018 resolves to abc_dialog_material_background which is part of the appcompat library.

This is the code invoking the error

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()

This is my module build.gradle file

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

All versions are retrieved from project build.gradle and are up-to-date.

like image 711
DarkerTV Avatar asked Dec 14 '22 15:12

DarkerTV


1 Answers

Found the problem after doing everything again step by step in a new project, it was an item in the application theme.

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

After removing that line or changing the value to a color defined in colors.xml, dialogs started to work again. And since I want the status bar to have the same color as the background I just changed it for this, getting the same result and working dialogs.

<item name="android:statusBarColor">?android:attr/colorBackground</item>
like image 198
DarkerTV Avatar answered Dec 18 '22 00:12

DarkerTV