android.support.v4.app.DialogFragment.show中的Nullpointer异常

时间:2016-07-25 09:00:20

标签: android android-fragments nullpointerexception color-picker

我有一个按钮(button1)。点击按钮,我试图显示colorpicker。 BaseViewable类扩展了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");
                    }
                });
      `

以下是ColorPickerDialog类:

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);
        }
    }
}

我无法弄清楚问题,虽然我看过很多帖子类似于同一个问题但无法解决。请帮忙

请检查我的日志:

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)

1 个答案:

答案 0 :(得分:0)

由于它是android.support.v4.app.DialogFragment,您应该使用getSupportFragmentManager()来显示对话框片段。

如果您从活动中展示:

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

如果您从片段中显示:

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

编辑1: 使用

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