0

I'm trying to implement af ColorPicker in my TextSelection toolbar/menu but when the button for the colorpicker is pressed in my app, I get this error saying:

java.lang.IllegalStateException: Activity has been destroyed 
at org.m.muddzboy.QuoteCreator.CustomTextSelectionMenu.onActionItemClicked(CustomTextSelectionMenu.java:81)

The error at line 81 is the following: cp.show(getFragmentManager(), "d");

For a better undstanding, this is where from I want to call the fragment:

enter image description here

And this is my custom class for the textselection menu:

public class CustomTextSelectionMenu extends Activity implements android.view.ActionMode.Callback {

EditText editText = MainActivity.mEditText;
ColorPickerDialogFrag2 cp;
public static final int DIALOG_ID1 = 1;
int currentcolor;


@Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
    MenuInflater inflater = mode.getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    menu.removeItem(android.R.id.selectAll);
    cp = new ColorPickerDialogFrag2();

    return true;
}

@Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
    return false;
}

@Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {


    int selectionStart = editText.getSelectionStart();
    int selectionEnd = editText.getSelectionEnd();
    if (selectionEnd > selectionStart) {
        Spannable str = editText.getText();
        boolean exists = false;
        StyleSpan[] styleSpans;

        switch (item.getItemId()) {
            case R.id.textcolor:
                if(currentcolor == 0){
                    currentcolor = editText.getCurrentTextColor();
                }
//LOOK HERE -------------
                cp = ColorPickerDialogFrag2.newInstance(DIALOG_ID1, null, null, currentcolor, false);
                cp.setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme);
                Log.d("TAG", "IM JUST ABOVE fragmentManager");

                cp.show(getFragmentManager(), "d");
                Log.d("TAG", "IM JUST UNDER fragmentManager!! SUCCSES!!");
//-------------------

                new ColorPickerDialogFrag2.ColorPickerDialogListener(){

                    @Override
                    public void onPreviewColorChanged(int dialogId, int color) {

                    }

                    @Override
                    public void onColorSelected(int dialogId, int color) {
                        if(dialogId == DIALOG_ID1) {
                            Spannable str = editText.getText();
                            str.setSpan(new ForegroundColorSpan(color), editText.getSelectionStart(), editText.getSelectionEnd(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                            editText.setSelection(editText.getSelectionStart(), editText.getSelectionEnd());
                            currentcolor = color;
                        }
                    }
                };
            break;

The dialog fragment class which I'm trying to call:

public class ColorPickerDialogFrag2 extends DialogFragment {

public interface ColorPickerDialogListener {
    void onPreviewColorChanged(int dialogId, int color); // CHANGE: added new method here.
    void onColorSelected(int dialogId, int color);
}


private int mDialogId = -1;
private ColorPickerView mColorPicker;
private ColorPickerDialogListener mListener;


public static ColorPickerDialogFrag2 newInstance(int dialogId, int initialColor) {
    return newInstance(dialogId, null, null, initialColor, false);
}

public static ColorPickerDialogFrag2 newInstance(
        int dialogId, String title, String okButtonText, int initialColor, boolean showAlphaSlider) {

    ColorPickerDialogFrag2 frag = new ColorPickerDialogFrag2();
    Bundle args = new Bundle();
    args.putInt("id", dialogId);
    args.putString("title", title);
    args.putString("ok_button", okButtonText);
    args.putBoolean("alpha", showAlphaSlider);
    args.putInt("init_color", initialColor);

    frag.setArguments(args);

    return frag;
}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDialogId = getArguments().getInt("id");
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    Log.d("color-picker-view", "onAttach()");

    // Check for listener in parent activity
    try {
        mListener = (ColorPickerDialogListener) activity;
    }
    catch (ClassCastException e) {
        e.printStackTrace();
        throw new ClassCastException("Parent activity must implement "
                + "ColorPickerDialogListener to receive result.");
    }
}



@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog d = super.onCreateDialog(savedInstanceState);


    d.requestWindowFeature(Window.FEATURE_NO_TITLE);
    d.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    return d;
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.costum_colorpickerview__dialog_color_picker, container);

    mColorPicker = (ColorPickerView)
            v.findViewById(R.id.custom_colorpickerview__color_picker_view);


    mColorPicker.setOnColorChangedListener(new OnColorChangedListener() {

        @Override
        public void onColorChanged(int newColor) {
            if (mListener != null) {
                mListener.onPreviewColorChanged(mDialogId, newColor); // CHANGE: send color through interface to listening activity.
            }
        }
    });


    if(savedInstanceState == null) {
        mColorPicker.setAlphaSliderVisible(
                getArguments().getBoolean("alpha"));

        int initColor = getArguments().getInt("init_color");

        mColorPicker.setColor(initColor, true);
    }


    return v;
}


//Når der trykkes på tilbageknappen på mobilen sættes farven og dialogen forsivnder
@Override
public void onCancel(DialogInterface dialog) {
    mListener.onColorSelected(mDialogId, mColorPicker.getColor());
    getDialog().dismiss();
}
  • Can you share your fragment please? – LordCommanDev Dec 09 '15 at 20:55
  • @Coeus Hi. What do you mean exactly (: ? The ColorPickerDialogFrag2 is the colorpicker fragment and is working when used other places in the app –  Dec 09 '15 at 22:55
  • @Coeus do you want me to send the entire code for the ColorPickerDialogFrag? –  Dec 10 '15 at 18:48
  • Yes please, verify if you have the onDetach() method in your fragment. I have been looking for the solution of your problem and I found a similar question but with tabs instead of option's menu. http://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed – LordCommanDev Dec 11 '15 at 22:10
  • Hi. I dont have an OnDetach() but there is a onAttach. The fragment class extends DialogFragment. I have posted the whole class now @Coeus –  Dec 12 '15 at 11:20
  • ok! First, sorry because I didn't notice what you said above. "cp.show(getFragmentManager(), "d")". Please can you try with getSupportFragmentManager() instead of getFragmentManager() – LordCommanDev Dec 12 '15 at 13:53
  • @Coeus I have tried that, but the it wont accept it, I tried to extend FragmentActivity, Fragment class of v4. etc. –  Dec 12 '15 at 16:04
  • @Coeus any news? Because I have tried a lot of things without succses! –  Dec 15 '15 at 18:16
  • Did you check the link posted above? It's look like a bug or something like that.. http://stackoverflow.com/questions/25185950/java-lang-illegalstateexception-activity-has-been-destroyed try to add the onDetach() method in you fragment please, let me know if works – LordCommanDev Dec 15 '15 at 19:39
  • @Coeus Yes i tried those solutions and the ones in the link in that thread without any succses. whatever I do, i keep getting error at only: `cp.show(getFragmentManager(), "d");` –  Dec 15 '15 at 20:19
  • 1
    @Coeus After many many hours of frustation and stress I can anonuce that I have finally solved the issue. I will paste the solution tomorrow. May the force be with you all! So relieving! –  Dec 15 '15 at 21:54
  • haha what a great notice! Long live to android! @Muddz – LordCommanDev Dec 15 '15 at 22:44

0 Answers0