Can't get String from EditText in Custom Alert

2019-09-07 23:18发布

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.

4条回答
smile是对你的礼貌
2楼-- · 2019-09-07 23:32

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

查看更多
Explosion°爆炸
3楼-- · 2019-09-07 23:37

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楼-- · 2019-09-07 23:42

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();
查看更多
Rolldiameter
5楼-- · 2019-09-07 23:45

Use this:

pin = (EditText)dialog.findViewById(R.id.insert_pin);
input = pin.getText().toString();
查看更多
登录 后发表回答