我正在尝试在TextSelection工具栏/菜单中实现af ColorPicker 但是当我的应用程序中按下了颜色选择器的按钮时,我收到此错误说:
java.lang.IllegalStateException: Activity has been destroyed
at org.m.muddzboy.QuoteCreator.CustomTextSelectionMenu.onActionItemClicked(CustomTextSelectionMenu.java:81)
第81行的错误如下: cp.show(getFragmentManager(), "d");
为了更好的未突出,这是我想要称之为片段的地方:
这是我的textselection菜单的自定义类:
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;
我想要调用的对话框片段类:
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();
}