How to access an item inside a DialogFragment? In my dialogue (View) there are also input fields when I try to access them in the "standard way" (as usual I refer to the elements of the fragment):

EditText text = (EditText)getActivity().findViewById(R.id.number_n); 

I get an exception that text = null and I can't access this element.

'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference

Below is the source of the fragment itself.

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { form = getActivity().getLayoutInflater() .inflate(R.layout.loginform, null); EditText text = (EditText)getActivity().findViewById(R.id.number_n); text.setText("123"); AlertDialog.Builder builder=new AlertDialog.Builder(getActivity()); return(builder.setTitle("Форма смены пользователя").setView(form) .setPositiveButton("Ок", this) .setNegativeButton("Отмена", null) .setNeutralButton("Забыли пароль?", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getActivity().getApplicationContext(), "remember", Toast.LENGTH_SHORT).show(); new CustomDialogFragmentRemember().show(getActivity().getFragmentManager(), "remember"); } }) .create()); } 
  • He does not ask you for text == null or not. Clearly says that text = null . As for the question, I do not see any textView.setText() - DevOma
  • Perhaps I put it wrong, edited - Heaven

2 answers 2

Instead

 form = getActivity().getLayoutInflater() .inflate(R.layout.loginform, null); EditText text = (EditText)getActivity().findViewById(R.id.number_n); text.setText("123"); 

try

 form = getActivity().getLayoutInflater() .inflate(R.layout.loginform, null); EditText text = form.findViewById(R.id.number_n); text.setText("123"); 

If you still need to change the view in the activation, then the answer from TITAN will do.

  • Your option came up, thank you) - Heaven

Try so, did not check.

Create a method in the Activity, for example

 public void setMyText(String string){ text.setText(string); } 

and in the dialog box

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { form = getActivity().getLayoutInflater() .inflate(R.layout.loginform, null); MyActivity myActivity = new MyActivity(); ((myActivity) getActivity()).setMyText("123"); AlertDialog.Builder builder=new AlertDialog.Builder(getActivity()); return(builder.setTitle("Форма смены пользователя").setView(form) .setPositiveButton("Ок", this) .setNegativeButton("Отмена", null) .setNeutralButton("Забыли пароль?", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getActivity().getApplicationContext(), "remember", Toast.LENGTH_SHORT).show(); new CustomDialogFragmentRemember().show(getActivity().getFragmentManager(), "remember"); } }) .create()); } 
  • one
    > MyActivity myActivity = new MyActivity(); NEVER do so - it creates an instance detached from the life (LifeCicle) of the application, which is fraught with annoying misunderstandings (aka NullPointerException, memory leak, etc.). - woesss
  • Not anymore!) - DevOma