当前位置:  开发笔记 > 编程语言 > 正文

调用Setter方法时自定义对话框片段崩溃

如何解决《调用Setter方法时自定义对话框片段崩溃》经验,为你挑选了0个好方法。

我有这样的CustomDialogFragment

public class CustomDialogFragment extends DialogFragment {

    private LinearLayout containerView;


    public static CustomDialogFragment newInstance() {
        CustomDialogFragment fragment = new EDActionSheet();
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Dialog dialog = new Dialog(getActivity());
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //MARK - containerView
        LinearLayout.LayoutParams containerViewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        containerView = new LinearLayout(dialog.getContext());
        containerView.setLayoutParams(containerViewLayoutParams);
        containerView.setOrientation(LinearLayout.VERTICAL);
        dialog.setContentView(containerView);
        DisplayMetrics displaymetrics = new DisplayMetrics();
        dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (int) (displaymetrics.widthPixels * 0.95);
        params.gravity = Gravity.BOTTOM;
        dialog.getWindow().setAttributes(params);

        return dialog;
}

public void setColor(int color) {
      containerView.setBackgroundColor(color);
}

从外面我称之为DialogFragment.

    CustomDialogFragment fragment = CustomDialogFragment.newInstance();
    fragment.setColor(ContextCompat.getColor(this, Color.BLUE));
    fragment.show(getFragmentManager(), "Dialog");

我得到了崩溃说

 testapp.android.testapp E/AndroidRuntime: FATAL EXCEPTION: main
         Process: id.testapp.android.testapp, PID: 5749
         java.lang.NullPointerException: Attempt to invoke virtual method 'void
 id.testapp.android.testapp.controls.CustomDialogFragment.setColor(int)'
 on a null object reference
         at 
id.testapp.android.testapp.controls.CustomDialogFragment.setColor(CustomDialogFragment.java:234)

有什么想法吗?

推荐阅读
ar_wen2402851455
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有