This is the general Dialog Fragment class from where I set the arguments into the 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();
}
}
This is how I am calling it in the activity
GeneralDialogFragment generalDialogFragment = new GeneralDialogFragment();
generalDialogFragment.newInstance("Test", "Its working good");
generalDialogFragment.show(getFragmentManager(), "dialog");
But I get a null pointer exception on onCreateDialog during setTitle(getArguments().getString("title"))
The method newInstance
is static, you don't need to create an object to reference it.
You should call newInstance
and get the reference to the Dialog:
GeneralDialogFragment generalDialogFragment = GeneralDialogFragment.newInstance("Test", "Its working good");
generalDialogFragment.show(getFragmentManager(), "dialog");