为什么应用程序在将颜色应用于 android 中的编辑文本文本时崩溃?

问题描述

private void ShowPicker() {
    AlertDialog colorPickerDialog= new ColorPickerDialog.Builder(getContext())
            .setTitle("ColorPicker Dialog")
            .setPreferenceName("MyColorPickerDialog")
            .setPositiveButton("Confirm",new ColorEnvelopeListener() {
                        @Override
                        public void onColorSelected(ColorEnvelope envelope,boolean fromUser) {
                            textEditBottomSheetCallback.SetTextColor(envelope.getColor());
                        }
                    })
            .setNegativeButton("cancel",new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface,int i) {
                            dialogInterface.dismiss();
                        }
                    })
            .attachAlphaSlideBar(true) // the default value is true.
            .attachBrightnessSlideBar(true)  // the default value is true.
            .setBottomSpace(12) // set a bottom space between the last slidebar and buttons.
            .show();
    colorPickerDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.blue));
    colorPickerDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.blue));
}
@Override
public void SetTextColor(int color) {
    edtTitle.setTextColor(ContextCompat.getColor(getContext(),color));
    edtText.setTextColor(ContextCompat.getColor(getContext(),color));
}

显示选择器方法中,我使用了skydove 颜色选择器库,在设置文本颜色方法中,我将颜色设置为编辑文本文本。当我从颜色选择器对话框中选择颜色时,应用程序崩溃。关于这个问题的任何建议。 Sky Dove Library

解决方法

未提供日志,但我猜测您是在对话框显示后为其应用颜色。试试这个:

private void ShowPicker() {
AlertDialog colorPickerDialog= new ColorPickerDialog.Builder(getContext())
        .setTitle("ColorPicker Dialog")
        .setPreferenceName("MyColorPickerDialog")
        .setPositiveButton("Confirm",new ColorEnvelopeListener() {
                    @Override
                    public void onColorSelected(ColorEnvelope envelope,boolean fromUser) {
                        textEditBottomSheetCallback.SetTextColor(envelope.getColor());
                    }
                })
        .setNegativeButton("cancel",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface,int i) {
                        dialogInterface.dismiss();
                    }
                })
        .attachAlphaSlideBar(true) // the default value is true.
        .attachBrightnessSlideBar(true)  // the default value is true.
        .setBottomSpace(12); // set a bottom space between the last slidebar and buttons.
        
colorPickerDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.blue));
colorPickerDialog.show();
}