0

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"))

Sudeep
  • 3
  • 4
  • How did this even compile? You don't return a Dialog except when `bundle == null` – Bö macht Blau Jan 21 '18 at 13:26
  • Oh sorry I put the wrong code. How about now? – Sudeep Jan 21 '18 at 13:30
  • 1
    You're not using `newInstance()` correctly. That method _returns_ a `GeneralDialogFragment` instance with the arguments set. It doesn't set the arguments on an existing instance. You need to assign that return to your `generalDialogFragment`, instead of directly instantiating an instance with `new` there. That is, `GeneralDialogFragment generalDialogFragment = GeneralDialogFragment.newInstance("Test", "Its working good");`. – Mike M. Jan 21 '18 at 13:37
  • 1
    Thanks Mike for you help. I still have a lot to learn. – Sudeep Jan 22 '18 at 04:18

2 Answers2

1

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");
Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
1

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.

Ben P.
  • 52,661
  • 6
  • 95
  • 123