Reputation: 3
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"))
Upvotes: 0
Views: 702
Reputation: 54204
As Juan Cruz Soler says, one problem is in how you are using newInstance()
. There is also a second problem, however.
Inside newInstance()
you have this line:
args.putString("title ", title);
You then try to read the title out of the arguments Bundle
with this line in onCreateDialog()
:
.setTitle(getArguments().getString("title"))
This won't work because your keys don't match. Even though it's just whitespace, "title "
and "title"
are not the same string. Delete the spaces from "title "
in your putString()
call, and this will be fixed.
Upvotes: 1
Reputation: 8254
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");
Upvotes: 1