我正在尝试将系统警报对话框按钮的颜色更改为自定义颜色。我一直在尝试使用此:

dialog.create().getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getColor(R.color.bluish_grey))
但是该应用程序崩溃了。
另外,dialog.getButton不起作用。只有set ...()方法。
而且,我还尝试在.getButton()之后使用dialog.show(),但是应用程序崩溃并显示以下错误日志。
我的警报对话框不是自定义的,只是系统对话框。因此,是否可以在不使用XML的情况下将警报对话框按钮的颜色设置为自定义颜色?
这是我想要实现的。
android - 在Android Kotlin中无法访问dialog.getButton()-LMLPHP
这是我目前拥有的:
android - 在Android Kotlin中无法访问dialog.getButton()-LMLPHP
更新:错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: no.company.app, PID: 26643
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setTextColor(int)' on a null object reference
        at no.meshtech.demokit.view.activity.MainActivity.showCancelDialog(MainActivity.kt:402)
        at no.meshtech.demokit.view.activity.MainActivity.startUpdate(MainActivity.kt:317)
        at no.meshtech.demokit.view.activity.MainActivity.onClick(MainActivity.kt:67)
        at android.view.View.performClick(View.java:7869)
        at android.widget.TextView.performClick(TextView.java:14958)
        at android.view.View.performClickInternal(View.java:7838)
        at android.view.View.access$3600(View.java:886)
        at android.view.View$PerformClick.run(View.java:29362)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8019)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

更新了2:
我创建警报的代码:
   override fun showCompletedDialog() {
        DfuBaseService.PROGRESS_COMPLETED

        val dialog = AlertDialog.Builder(this)

            .setTitle(getString((R.string.dfu_completed_title)))
            .setMessage(getString(R.string.dfu_completed_message))
            .setPositiveButton(R.string.dfu_completed_ok) { dialog, _ ->
                transaction(listFragment, R.id.cvCentral)
                supportFragmentManager.findFragmentById(R.id.upperContainer)?.let {
                    supportFragmentManager.beginTransaction().remove(it).commit()
                    setNavigationTitle(getString(R.string.title_header_devices))
                }
                upperContainer.visibility = View.GONE

                Handler().postDelayed({
                    onBackPressed()
                    dialog.dismiss()
                }, 400)
            }


        dialog.setCancelable(false)
        dialog.show()
    }

最佳答案

您应该在setTextColor之后添加 show()

    val alertDialog = AlertDialog.Builder(this).create()
    alertDialog.setTitle(getString((R.string.app_name)))
    alertDialog.setMessage(getString(R.string.app_name))
    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "RESUME"
    ) { dialog, which -> dialog.dismiss() }

    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "ABORT"
    ) { dialog, which -> dialog.dismiss() }

    alertDialog.show()
    alertDialog.setCancelable(false)
    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(this,R.color.colorPrimary))
    alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(this,R.color.colorAccent))

09-28 00:09