通用对话框片段 class 中的 onCreateDialog returns null 每当我尝试获取我在调用 activity 中设置的参数时

onCreateDialog in the general dialog fragment class returns null whenever I try to fetch the arguments which I set in my calling activity

这是通用对话框片段class,我从中将参数设置到包中

public class GeneralDialogFragment extends BaseDialogFragment<GeneralDialogFragment.OnDialogFragmentClickListener> {


    public interface OnDialogFragmentClickListener {
        public void onClicked(GeneralDialogFragment dialogFragment);

        public void onCancelClicked(GeneralDialogFragment dialogFragment);

    }


    public static GeneralDialogFragment newInstance(String title, String message) {
        GeneralDialogFragment dialog = new GeneralDialogFragment();
        Bundle args = new Bundle();
        args.putString("title  ", title);
        args.putString("message", message);
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new AlertDialog.Builder(getActivity())
                .setTitle(getArguments().getString("title"))
                .setMessage(getArguments().getString("message"))
                .setCancelable(false)
                .create();
    }


}

这就是我在 activity

中的调用方式
GeneralDialogFragment generalDialogFragment = new GeneralDialogFragment();
                    generalDialogFragment.newInstance("Test", "Its working good");
                    generalDialogFragment.show(getFragmentManager(), "dialog");

但是我在 setTitle(getArguments().getString("title"))

期间在 onCreateDialog 上得到一个空指针异常

方法newInstance是静态的,您不需要创建对象来引用它。
您应该调用 newInstance 并获取对对话框的引用:

GeneralDialogFragment generalDialogFragment = GeneralDialogFragment.newInstance("Test", "Its working good");
generalDialogFragment.show(getFragmentManager(), "dialog");

正如 Juan Cruz Soler 所说,一个问题在于您如何使用 newInstance()。然而,还有第二个问题。

newInstance() 里面你有这一行:

args.putString("title  ", title);

然后您尝试使用 onCreateDialog() 中的这一行从参数 Bundle 中读取标题:

.setTitle(getArguments().getString("title"))

这行不通,因为您的密钥不匹配。尽管它只是空格,但 "title ""title" 不是同一个字符串。在您的 putString() 调用中删除 "title " 中的空格,这将得到修复。