You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义AlertDialog出现大量ResourceCompat错误的解决方法

自定义AlertDialog出现ResourceCompat错误的解决方法

我创建了一个简单的自定义AlertDialog,却出现大量与ResourceCompat相关的错误。相关代码和错误日志如下:

AlertDialog创建代码:

class CustomLoadingDialog(
    activity: Activity
) {

    private val dialog: Dialog
    private val view: View
    private val textViewDialogMessage: TextView

    init {
        val builder = AlertDialog.Builder(activity)
        view = activity.layoutInflater.inflate(R.layout.custom_loading_dialog,null)
        builder.setView(view)
        builder.setCancelable(true)
        dialog = builder.create()
        textViewDialogMessage= view.findViewById(R.id.textViewDialogMessage)
    }

    fun showDialog() {
        dialog.show()
    }

    fun updateText(text: String) {
        textViewDialogMessage.text = text
    }

    fun dismissDialog() {
        dialog.dismiss()
    }
}

在Fragment中使用:

private val customLoadingDialog by lazy {
    CustomLoadingDialog(requireActivity())
}

private lateinit var show: Button


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    show = view.findViewById(R.id.show)

    show.setOnClickListener {
        customLoadingDialog.showDialog()
        customLoadingDialog.updateText("hello")
    }

}

custom_loading_dialog布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_marginTop="64dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminateTint="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textViewDialogMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="@+id/progressBar"
        app:layout_constraintStart_toStartOf="@+id/progressBar"
        app:layout_constraintTop_toBottomOf="@+id/progressBar" />

    <Button
        android:id="@+id/buttonDialogDismiss"
        style="@style/Widget.Material3.Button.TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:text="Cancel"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textViewDialogMessage" />


</androidx.constraintlayout.widget.ConstraintLayout>

错误日志:

W/ResourcesCompat: Failed to inflate ColorStateList, leaving it to the framework
    java.lang.UnsupportedOperationException: Failed to resolve attribute at index 0: TypedValue{t=0x2/d=0x7f03010b a=-1}
        at android.content.res.TypedArray.getColor(TypedArray.java:529)
        at androidx.core.content.res.ColorStateListInflaterCompat.inflate(ColorStateListInflaterCompat.java:160)
        ...(省略重复错误)

错误原因

错误根源是布局中的Button使用了Widget.Material3.Button.TextButton样式,但你的应用主题未继承Material3主题,导致样式中的属性无法被解析,进而触发ColorStateList加载失败的警告。

解决方法

方法1:全局使用Material3主题

打开res/values/styles.xml(或res/values/themes.xml),将应用主题修改为继承Material3主题:

<style name="AppTheme" parent="Theme.Material3.Light.NoActionBar">
    <!-- 保留你的自定义主题属性 -->
</style>

确保在AndroidManifest.xml中应用该主题:

<application
    android:theme="@style/AppTheme"
    ...>
</application>

方法2:为Dialog单独指定Material3主题

如果不想全局切换Material3主题,可在创建AlertDialog时单独指定Material3主题:
修改CustomLoadingDialog的初始化代码:

init {
    // 使用Material3的Dialog主题
    val builder = AlertDialog.Builder(activity, R.style.Theme_Material3_Light_Dialog_Alert)
    view = activity.layoutInflater.inflate(R.layout.custom_loading_dialog,null)
    builder.setView(view)
    builder.setCancelable(true)
    dialog = builder.create()
    textViewDialogMessage= view.findViewById(R.id.textViewDialogMessage)
}

若系统未提供该主题,可在styles.xml中手动定义:

<style name="Theme.Material3.Light.Dialog.Alert" parent="Theme.Material3.Dialog.Alert">
    <!-- 自定义属性 -->
</style>

方法3:替换Button样式(放弃Material3)

将布局中Button的Material3样式替换为系统默认的无边界按钮样式:

<Button
    android:id="@+id/buttonDialogDismiss"
    style="?android:attr/borderlessButtonStyle"
    ... />

也可以自定义一个不依赖Material3的按钮样式,避免属性解析问题。


内容的提问来源于stack exchange,提问作者Qwe Qwe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 18:22:06