3

Here is my code:

public void openDialog(){

    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    AlertDialog alert = alertDialogBuilder.create();
    final EditText a = (EditText) alert.findViewById(R.id.kekekeke);

    LayoutInflater inflater = getLayoutInflater();
    alertDialogBuilder.setView(inflater.inflate(R.layout.list_example, null));
    alertDialogBuilder
        .setMessage("Enter a New Name")
        .setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
            }
        });
}

My problem is that I got below error :

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

I'm confused because I thought that I already accessed it through the edittext.

Andrea Ebano
  • 563
  • 1
  • 4
  • 16
K. Lok
  • 35
  • 5

4 Answers4

5

You can reference it like below

LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.list_example, null);
EditText a = (EditText) v.findViewById(R.id.kekekeke);
alertDialogBuilder.setView(v);
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
  • 1
    Thank you, I'll accept it when 7 minutes have passed, could you specify why this works so I could understand it better? – K. Lok Jul 04 '19 at 05:19
  • What you were trying is to get the reference of EditText before setting the customView on it. What I have done is first get the reference of the layout and then try to get the EditText from the layout – Rahul Khurana Jul 04 '19 at 05:21
0

Try this,

public void openDialog(){

final AlertDialog.Builder alertDialogBuilder =
        new AlertDialog.Builder(this);

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.list_example, null);
alertDialogBuilder.setView(view);


final EditText a = (EditText) view.findViewById(R.id.kekekeke);


alertDialogBuilder    .setMessage("Enter a New Name")
        .setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {


myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());


            }
        })

AlertDialog alert = alertDialogBuilder.create();
alert.show();

You should inflate the view first

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
0

Problem is your alert dialog has not set the view when you get the reference from it, so it does not have anything until you call setview, so you are calling it before and it is providing you nothing so you get the null pointer

public void openDialog(){

    final AlertDialog.Builder alertDialogBuilder =
            new AlertDialog.Builder(this);

    AlertDialog alert = alertDialogBuilder.create();


    LayoutInflater inflater = getLayoutInflater();
    alertDialogBuilder.setView(inflater.inflate(R.layout.list_example, null));
final EditText a = (EditText) alert.findViewById(R.id.kekekeke);
   alertDialogBuilder    .setMessage("Enter a New Name")
            .setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());


                }
            })
Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42
0

Problem is your alert dialog has not to the view when you try to get reference from view, so it does not have anything until you set your view to AlertDialog, So it returns NullPointerException,

Just replace this code with your code

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

AlertDialog alert = alertDialogBuilder.create();

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.list_example, null);
final EditText a = (EditText) view.findViewById(R.id.kekekeke);

alertDialogBuilder.setView(view);

alertDialogBuilder
    .setMessage("Enter a New Name")
    .setPositiveButton("Edit Name", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            myRef.child(Utils.object.getKey()).child("sfasf").setValue(a.getText().toString());
        }
    });
Raza
  • 19
  • 3