0

I created a DialogFragment. The Dialog got a EditText and I call it from the dialog xml. But there is the error: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

New in this code:

    AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

and

final EditText editText = (EditText) alertDialog.findViewById(R.id.editTextPasswordDeveloper);

My Code:

public class DeveloperLoginDialogFragment extends DialogFragment {

EditText editText;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final LayoutInflater inflaterDialog = getActivity().getLayoutInflater();

    AlertDialog alertDialog;
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    alertDialog = builder.create();

   // final AlertDialog alertDialog = new AlertDialog.Builder(getActivity());
    builder.setCancelable(false);
    builder.setView(inflaterDialog.inflate(R.layout.alertdialog_developer, null));
    final EditText editText = (EditText) alertDialog.findViewById(R.id.editTextPasswordDeveloper);
    builder.setTitle("Entwickler-Login");
    builder.setMessage("Um dich als Entwickler zu verifizieren, gib den Entwickler-Code bitte unten ein.");
    builder.setPositiveButton("Verifizieren", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(editText.getText().toString().equals("7846")) {
                dialog.dismiss();
                Toast.makeText(getActivity().getApplicationContext(),"Erfolgreich verifiziert!", Toast.LENGTH_LONG).show();
            }
        }
    });
    builder.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
        }
    });
    return builder.create();
}

}

N.Preusche
  • 37
  • 1
  • 2
  • 7
  • 1
    You are calling `findViewById` on an object before assigning any value to it. Perhaps you wanna assign value of `inflaterDialog.inflate` to a View variable and then do `view.findViewById` – Sourabh Mar 01 '17 at 16:20

2 Answers2

1

editText = (EditText) editText.findViewById(R.id.editTextPasswordDeveloper);

You are calling findViewById() on editText which is null. You probably want to use EditText editText = (EditText) alertDialog.findViewById(R.id.label_field); instead.

Note the use of alertDialog rather than editText.

Josh Laird
  • 6,974
  • 7
  • 38
  • 69
1

the problem is simple you are using the edit text to find a view. You have two options either inflate a layout then use findViewById or use the alertDialog.findViewById();

try this.

AlertDialog.Builder builder = new AlertDialog.Builder(FeedFragment.this.getActivity());
    builder.setView(R.layout.layout_edit_review);
    builder.setCancelable(true);
    Dialog dialog = builder.create();
    dialog.show();
    TextInputEditText text_edit = (TextInputEditText) dialog.findViewById(R.id.editReview);

try something like that.

Remario
  • 3,813
  • 2
  • 18
  • 25