Darren
Darren

Reputation: 10398

DialogFragment null pointer when setting OnCancelListener

I am trying to set listeners on a DialogFragment, but keep getting a null pointer exception.

This is my DialogFragment subclass:

public static class TermsFragment extends DialogFragment {
        TextView t;
        private static String terms;

        static TermsFragment newInstance(String termsCond) {
            TermsFragment f = new TermsFragment();
            terms = termsCond;
            return f;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            getDialog().setCanceledOnTouchOutside(true);
            View v = inflater.inflate(R.layout.termsandconditions, container, false);

            getDialog().setTitle("Terms and Conditions");

            t = (TextView) v.findViewById(R.id.termsText);

            t.setText(terms);

            return v;
        }
    }

and I am calling it from my main SherlockFragment like this:

TermsFragment newFragment = TermsFragment.newInstance(terms);
newFragment.show(getFragmentManager(), "dialog");

This shows my dialog fine. When I try adding a button listener like this:

TermsFragment newFragment = TermsFragment.newInstance(terms);
Button cancel = (Button) newFragment.getDialog().findViewById(R.id.btnCancel);
        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.e(TAG, "Clicked");

            }
        });

or a general onCancel listener like this:

newFragment.getDialog().setOnCancelListener(new Dialog.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                showLoadingView(false);

            }
        });

My app crashed with a null pointer exception before showing the dialog.

What am I doing wrong?

Upvotes: 2

Views: 973

Answers (1)

TronicZomB
TronicZomB

Reputation: 8747

Instead of using public View onCreateView why don't you try using the public Dialog onCreateDialog. Here is an example:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    Bundle args = getArguments();
    currentName = args.getString(ARG_CURRENT_NAME);

    builder.setView(inflater.inflate(R.layout.name_dialog, null));
    builder.setTitle("Rename Rapper Program");
    builder.setMessage("Enter a new name for " + currentName + ":"); 
    builder.setPositiveButton("Rename", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            newName = (EditText) getDialog().findViewById(R.id.new_name);
            newProgName = newName.getText().toString();
            mRename.renameProgram(currentName, newProgName);
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dismiss();
        }
    });

    return builder.create();    
}

You will continue to use the .show() as you have it now and you can model this after what you are trying to achieve.

Upvotes: 1

Related Questions