Can't get String from EditText in Custom Alert

2019-09-07 23:05发布

问题:

I am in an impasse regarding getting the text from a custom AlertDialog. I get an error "NullPointerException". I have moved defining the variable containing the EditText in the AlertDialog, but I get the same error.

My layout item in it's XML "pin.xml"

<EditText
    android:id="@+id/insert_pin"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:ems="10"
    android:gravity="center"
    android:inputType="numberPassword"
    android:maxLength="4" />

The AlertDialog

        new AlertDialog.Builder(this)
        .setView(inflater.inflate(R.layout.pin, null))
        .setTitle("Save PIN")
        .setPositiveButton("Save", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                pin = (EditText) findViewById(R.id.insert_pin);
                //here I get the Error. Apparently, it can't get the value
                input = pin.getText().toString();

                dialog.cancel();
                go();
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                dialog.cancel();
                finish();
            }
        })
        .show();

Any help would be vastly appreciated.

回答1:

you have to change your code as you are find the EditText Field object using findViewById() but you should findViewById() with respect to Dialog View.

Change your code as below:

View v = inflater.inflate(R.layout.pin, null);


new AlertDialog.Builder(this)
        .setView(v)
        .setTitle("Save PIN")
        .setPositiveButton("Save", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                pin = (EditText)v.findViewById(R.id.insert_pin);
                //here I get the Error. Apparently, it can't get the value
                input = pin.getText().toString();

                dialog.cancel();
                go();
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                dialog.cancel();
                finish();
            }
        })
        .show();


回答2:

use

pin = (EditText)v.findViewById(R.id.insert_pin);
input = pin.getText().toString();

for getting text from insert_pin EditText you will need to use dialog context



回答3:

write your code like this

    AlertDialog alert=new AlertDialog.Builder(this);

    alert.setView(inflater.inflate(R.layout.pin, null));
    alert.setTitle("Save PIN");

    alert.setPositiveButton("Save", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
            pin = (EditText)alert.findViewById(R.id.insert_pin);
            //error will be solved
            input = pin.getText().toString();

            dialog.cancel();
            go();
        }
    });
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int id){
            dialog.cancel();
            finish();
        }
    });
    alert.show();


回答4:

Use this:

pin = (EditText)dialog.findViewById(R.id.insert_pin);
input = pin.getText().toString();