Good day. There is such a method:

public void createURLLinkDialog(){ final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.dialog_url, null)) .setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { EditText et = (EditText) getActivity().findViewById(R.id.push_url_link); //TODO: ERROR ON BUTTON CLICK! urlToImage = et.getText().toString(); } }); builder.create(); builder.show(); } 

Actually, the point is that this method lies in the fragment that lies in the container of activation. As can be seen from the method, own custom markup is used for the dialogue. How to access the xml markup elements of the created dialog from the fragment?

Stacktrace

  java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at ru.timuruktus.newsletters.View.Fragments.PushPostFragment$3.onClick(PushPostFragment.java:165) 

    1 answer 1

    You need to get the View object of your markup after the inflate and look for your widgets in it, and not in activations or other strange places:

     final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final LayoutInflater inflater = getActivity().getLayoutInflater(); final View view = inflater.inflate(R.layout.dialog_url, null); builder.setView(view) .setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { EditText et = (EditText) view.findViewById(R.id.push_url_link); urlToImage = et.getText().toString(); } }); builder.create(); builder.show();