jaredrummler / ColorPicker

A highly customizable color picker for Android
Apache License 2.0
795 stars 172 forks source link

Possible bug: Fatal Exception: java.lang.IllegalStateException: The activity must implement ColorPickerDialogListener #67

Open AndroidDeveloperLB opened 6 years ago

AndroidDeveloperLB commented 6 years ago

I'm using ColorPreferenceCompat , and recently I got this crash from the play console:

Fatal Exception: java.lang.IllegalStateException: The activity must implement ColorPickerDialogListener at com.jaredrummler.android.colorpicker.c.a(ColorPickerDialog.java:19583) at com.jaredrummler.android.colorpicker.c$3.onClick(ColorPickerDialog.java:190) at androidx.appcompat.app.AlertController$b.handleMessage(AlertController.java:167) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6626) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

screencapture-console-firebase-google-project-lwp-plus-crashlytics-app-android-com-lb-lwp_plus-issues-5bf84c2df8b88c2963275d39-2018-11-24-12_51_08

link: https://console.firebase.google.com/project/lwp-plus/crashlytics/app/android:com.lb.lwp_plus/issues/5bf84c2df8b88c2963275d39?time=last-seven-days&sessionId=5BF8338A006500010FAF6123A067FB12_DNE_0_v2

I didn't set any special listener. Right on onCreatePreferences, I just call : setOnPreferenceChangeListener

thelittlefireman commented 4 years ago

Hi, same issue.

Fatal Exception: java.lang.IllegalStateException: The activity must implement ColorPickerDialogListener
       at com.jaredrummler.android.colorpicker.ColorPickerDialog.onColorSelected(ColorPickerDialog.java:583)
       at com.jaredrummler.android.colorpicker.ColorPickerDialog.access$000(ColorPickerDialog.java:68)
       at com.jaredrummler.android.colorpicker.ColorPickerDialog$2.onClick(ColorPickerDialog.java:190)
       at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:224)
       at android.app.ActivityThread.main(ActivityThread.java:7520)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

I think you should add the setColorPickerDialogListener(....) to the builder because there is a thread concurrency and sometimes the listener is null ==> if (colorPickerDialogListener != null)

Original source code :

private void onColorSelected(int color) {
    if (colorPickerDialogListener != null) {
      Log.w(TAG, "Using deprecated listener which may be remove in future releases");
      colorPickerDialogListener.onColorSelected(dialogId, color);
      return;
    }
    Activity activity = getActivity();
    if (activity instanceof ColorPickerDialogListener) {
      ((ColorPickerDialogListener) activity).onColorSelected(dialogId, color);
    } else {
      throw new IllegalStateException("The activity must implement ColorPickerDialogListener");
    }
  }

My code :

ColorPickerDialog colorPickerDialog = ColorPickerDialog.newBuilder()
                    .setColorShape(CIRCLE)
                    .setDialogType(ColorPickerDialog.TYPE_PRESETS)
                    .setAllowCustom(true)
                    .setAllowPresets(true)
                    .setColor(mEDTColor)
                    .create();
            colorPickerDialog.setColorPickerDialogListener(new ColorPickerDialogListener() {
                @Override
                public void onColorSelected(int dialogId, int color) {
                    mEDTColor = color;
                    mCircularImageViewEDTColor.setImageDrawable(new ColorDrawable(mEDTColor));
                }

                @Override
                public void onDialogDismissed(int dialogId) {

                }
            });
            colorPickerDialog.show(getSupportFragmentManager(), "COLOR_PICKER_EDT");
AndroidDeveloperLB commented 4 years ago

@thelittlefireman Is this a part of your own fork?

thelittlefireman commented 4 years ago

@AndroidDeveloperLB Nop i use the official repo implementation 'com.jaredrummler:colorpicker:1.1.0' :)

AndroidDeveloperLB commented 4 years ago

@thelittlefireman Is this a working workaround? Can you please show a sample of how to use it (zip file of a POC project) ?

thelittlefireman commented 4 years ago

This is not a workaround. I just show on the original code where the issue seems to be.

A small possible workaround is to implement implements ColorPickerDialogListener on the activity and use colorPickerDialog.setColorPickerDialogListener(this);

AndroidDeveloperLB commented 4 years ago

@thelittlefireman Sorry. Can you please show a POC of this, then?