Android 每当我试图获取在调用活动中设置的参数时,general dialog片段类中的onCreateDialog返回null

Android 每当我试图获取在调用活动中设置的参数时,general dialog片段类中的onCreateDialog返回null,android,nullpointerexception,dialogfragment,Android,Nullpointerexception,Dialogfragment,这是我将参数设置到bundle中的常规对话框片段类 public class GeneralDialogFragment extends BaseDialogFragment<GeneralDialogFragment.OnDialogFragmentClickListener> { public interface OnDialogFragmentClickListener { public void onClicked(GeneralDialogFra

这是我将参数设置到bundle中的常规对话框片段类

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


}

但是我在setTitle(getArguments().getString(“title”))期间在onCreateDialog上遇到一个空指针异常。

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

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

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

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

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

newInstance()
中,您有以下行:

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


这不起作用,因为你的钥匙不匹配。即使只是空白,
“title”
“title”
也不是同一个字符串。在
putString()
调用中删除
“title”
中的空格,这将得到修复。

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

newInstance()
中,您有以下行:

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


这不起作用,因为你的钥匙不匹配。即使只是空白,
“title”
“title”
也不是同一个字符串。在
putString()
调用中删除
“title”
中的空格,这将得到修复。

这是如何编译的?除非
bundle==null
哦,对不起,我输入了错误的代码,否则不会返回对话框。现在呢?您没有正确使用
newInstance()
。该方法返回一个带有参数集的
GeneralDialogFragment
实例。它不会在现有实例上设置参数。您需要将该返回分配给
generalDialogFragment
,而不是直接在那里用
new
实例化实例。也就是说,
GeneralDialogFragment-GeneralDialogFragment=GeneralDialogFragment.newInstance(“测试”,“它工作正常”)。谢谢迈克的帮助。我还有很多东西要学。这是怎么编译的?除非
bundle==null
哦,对不起,我输入了错误的代码,否则不会返回对话框。现在呢?您没有正确使用
newInstance()
。该方法返回一个带有参数集的
GeneralDialogFragment
实例。它不会在现有实例上设置参数。您需要将该返回分配给
generalDialogFragment
,而不是直接在那里用
new
实例化实例。也就是说,
GeneralDialogFragment-GeneralDialogFragment=GeneralDialogFragment.newInstance(“测试”,“它工作正常”)。谢谢迈克的帮助。我还有很多东西要学。谢谢你Ben指出这一点。我没有看到。谢谢你的帮助谢谢你Ben指出了这一点。我没有看到。谢谢你的帮助谢谢胡安。做出了改变。很好,谢谢你,胡安。做出了改变。效果很好。
args.putString("title  ", title);
.setTitle(getArguments().getString("title"))