MayTheSchwartzBeWithYou
MayTheSchwartzBeWithYou

Reputation: 1177

Can't get String from EditText in Custom AlertDialog

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.

Upvotes: 0

Views: 2517

Answers (4)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

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

Upvotes: 1

TNR
TNR

Reputation: 5869

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();

Upvotes: 4

URAndroid
URAndroid

Reputation: 6277

Use this:

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

Upvotes: 0

Nirav Tukadiya
Nirav Tukadiya

Reputation: 3417

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();

Upvotes: 0

Related Questions