1

I have a button(button1). On click of the button, I am trying to display colorpicker. BaseViewable class extends Fragment.

 public class Settingspreferences extends BaseViewable {

    @Override
    public View createView(final Context context, Object id, UserData extraData, IViewable viewable) {

        super.createView(context, id, extraData, viewable);


        final ColorPickerDialog colorPickerDialog = new ColorPickerDialog();
        colorPickerDialog.initialize(R.string.dialog_title, new int[]{Color.CYAN, Color.LTGRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED,
                Color.GRAY, Color.YELLOW}, Color.YELLOW, 3, 2);

        colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() {

            @Override
            public void onColorSelected(int color) {
                Toast.makeText(context, "selectedColor : " + color, Toast.LENGTH_SHORT).show();

            }
        });


        LinearLayout list_row2 = null;
        TextView calenderItemTV2 = null;
        ImageView calenderItemIV2 = null;
        Button calenderCB2 = null;

        LinearLayout parentLL2 = (LinearLayout) root.findViewById(R.id.rowHolder3);

                calenderCB2 = (Button) list_row2.findViewById(R.id.calenderCB2);

                calenderCB2.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        colorPickerDialog.show(getActivity().getSupportFragmentManager(), "colorpicker");
                    }
                });
      `

Below is the ColorPickerDialog class:

public class ColorPickerDialog extends DialogFragment implements ColorPickerSwatch.OnColorSelectedListener {
    protected AlertDialog mAlertDialog;
    protected int[] mColors = null;
    protected int mColumns;
    protected ColorPickerSwatch.OnColorSelectedListener mListener;
    private ColorPickerPalette mPalette;
    private ProgressBar mProgress;
    protected int mSelectedColor;
    protected int mSize;
    protected int mTitleResId = R.string.color_picker_default_title;

    private void refreshPalette() {
        if ((this.mPalette != null) && (this.mColors != null))
            this.mPalette.drawPalette(this.mColors, this.mSelectedColor);
    }

    public void initialize(int titleId, int[] colors, int selectedColor, int columns, int size) {
        setArguments(titleId, columns, size);
        setColors(colors, selectedColor);
    }

    public void onColorSelected(int selectedColor) {
        if (this.mListener != null)
            this.mListener.onColorSelected(selectedColor);
        if ((getTargetFragment() instanceof ColorPickerSwatch.OnColorSelectedListener))
            ((ColorPickerSwatch.OnColorSelectedListener) getTargetFragment()).onColorSelected(selectedColor);
        if (selectedColor != this.mSelectedColor) {
            this.mSelectedColor = selectedColor;
            this.mPalette.drawPalette(this.mColors, this.mSelectedColor);
        }
        dismiss();
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (getArguments() != null) {
            this.mTitleResId = getArguments().getInt("title_id");
            this.mColumns = getArguments().getInt("columns");
            this.mSize = getArguments().getInt("size");
        }
        if (bundle != null) {
            this.mColors = bundle.getIntArray("colors");
            this.mSelectedColor = ((Integer) bundle.getSerializable("selected_color")).intValue();
        }
    }

    public Dialog onCreateDialog(Bundle bundle) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker_dialog, null);
        this.mProgress = ((ProgressBar) view.findViewById(android.R.id.progress));
        this.mPalette = ((ColorPickerPalette) view.findViewById(R.id.color_picker));
        this.mPalette.init(this.mSize, this.mColumns, this);
        if (this.mColors != null)
            showPaletteView();
        this.mAlertDialog = new AlertDialog.Builder(getActivity()).setTitle(this.mTitleResId).setView(view).create();
        return this.mAlertDialog;
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putIntArray("colors", this.mColors);
        bundle.putSerializable("selected_color", Integer.valueOf(this.mSelectedColor));
    }

    public void setArguments(int titleId, int columns, int size) {
        Bundle bundle = new Bundle();
        bundle.putInt("title_id", titleId);
        bundle.putInt("columns", columns);
        bundle.putInt("size", size);
        setArguments(bundle);
    }

    public void setColors(int[] colors, int selected) {
        if ((this.mColors != colors) || (this.mSelectedColor != selected)) {
            this.mColors = colors;
            this.mSelectedColor = selected;
            refreshPalette();
        }
    }

    public void setOnColorSelectedListener(ColorPickerSwatch.OnColorSelectedListener onColorSelectedListener) {
        this.mListener = onColorSelectedListener;
    }

    public void showPaletteView() {
        if ((this.mProgress != null) && (this.mPalette != null)) {
            this.mProgress.setVisibility(View.GONE);
            refreshPalette();
            this.mPalette.setVisibility(View.VISIBLE);
        }
    }

    public void showProgressBarView() {
        if ((this.mProgress != null) && (this.mPalette != null)) {
            this.mProgress.setVisibility(View.VISIBLE);
            this.mPalette.setVisibility(View.GONE);
        }
    }
}

I am not able to figure it out the problem, although I have seen many post which resembles the same issue but couldn't resolve. Please help

Please check my logs:

07-25 14:44:54.113 25526-25526/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
07-25 14:44:54.113 25526-25526/? W/System.err:     at android.support.v4.app.DialogFragment.show(DialogFragment.java:137)
07-25 14:44:54.113 25526-25526/? W/System.err:     at com.sap.rex.ui.CalendarSettingspreferences$2.onClick(CalendarSettingspreferences.java:258)
07-25 14:44:54.113 25526-25526/? W/System.err:     at android.view.View.performClick(View.java:5246)
07-25 14:44:54.113 25526-25526/? W/System.err:     at android.widget.TextView.performClick(TextView.java:10573)
07-25 14:44:54.113 25526-25526/? W/System.err:     at android.view.View$PerformClick.run(View.java:21256)
07-25 14:44:54.113 25526-25526/? W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
07-25 14:44:54.113 25526-25526/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
07-25 14:44:54.113 25526-25526/? W/System.err:     at android.os.Looper.loop(Looper.java:145)
07-25 14:44:54.113 25526-25526/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6912)
07-25 14:44:54.113 25526-25526/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-25 14:44:54.113 25526-25526/? W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
07-25 14:44:54.113 25526-25526/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
07-25 14:44:54.113 25526-25526/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
user6434454
  • 21
  • 1
  • 5

1 Answers1

0

Since it is a android.support.v4.app.DialogFragment, you should use getSupportFragmentManager() for showing dialog fragment.

If you are showing from activity :

colorPickerDialog.show(getSupportFragmentManager(), "colorpicker");

If you are showing from Fragment :

colorPickerDialog.show(getActivity().getSupportFragmentManager(), "colorpicker");

Edit 1: Use

colorPickerDialog.show(getChildFragmentManager(), "colorpicker");

Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32