Search code examples
androidandroid-layoutnullpointerexceptionandroid-dialogfragment

DialogFragment - Where to access layout components [i.e. editText.setText("")]


after doing a lot of research and trying (i think) every lifecycle method i could find, i´m really stuck:

What i want: Basically i have this DialogFragment ("ProfileUsernameDialogFragment")which should present my layout file ("fragment_profile_header_username_dialog"). in this layout file i have some TextViews and some EditTexts. I want to set the text of these EditTexts to some value i obtain in my programm, so setting it beforehand is not possible.

What i have:

public class ProfileUsernameDialogFragment extends DialogFragment {

...

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    EditText usernameText = (EditText) this.getView().findViewById(R.id.username_field);
    usernameText.setText("TEST");
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Log.d(AppController.LOG_STRING, this.getClass().toString() + " - onCreateDialog  - START");
    AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
    View view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_profile_header_username_dialog, null);
    builder.setView(view);
    builder.setPositiveButton(SAVE_SETTINGS, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {
            // Tried this too:
            // EditText usernameText = (EditText)
            // ProfileUsernameDialogFragment.this.getView().findViewById(R.id.forename_label);
            // usernameText.setText("TEST");
        }
    });
    builder.setNegativeButton(ABORT_ACTION, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
        }
    });
    builder.setTitle(CHANGE_NAME_SETTINGS);
    Dialog dialog = builder.create();
    return dialog;
}
}

What i get: is a NullPointerException, no matter which position for the findViewById(R.id.username_field) i tried. (I also tried onCreate and some other methods, i start thinking i might have another problem than the code location, but i really hav no clue) I tried to follow at least 6 similar questions about NullPointers in DialogFragments so i guess i need advanced help ^^

i should add i´m completely new to android so please be nice, but convention tipps etc. are very welcome! thx in advance :)


Solution

  • Following is what you need:

    mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
    
            @Override
            public void onShow(final DialogInterface dialog) {
    
                final Button b = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
                b.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(final View view) {
    
                        v.findViewById(R.id...)
                    }
                });
            }
        });
                });
            }
        });