Android Can';无法从自定义AlertDialog中的EditText获取字符串

Android Can';无法从自定义AlertDialog中的EditText获取字符串,android,android-edittext,Android,Android Edittext,关于从自定义AlertDialog获取文本,我陷入了僵局。我得到一个错误“NullPointerException”。 我已经在AlertDialog中定义了包含EditText的变量,但是我得到了相同的错误 我的布局项在它的XML“pin.XML”中 任何帮助都将不胜感激。使用 pin = (EditText)v.findViewById(R.id.insert_pin); input = pin.getText().toString(); 要从insert_pinEditText获取文本,

关于从自定义AlertDialog获取文本,我陷入了僵局。我得到一个错误“NullPointerException”。 我已经在AlertDialog中定义了包含EditText的变量,但是我得到了相同的错误

我的布局项在它的XML“pin.XML”中

任何帮助都将不胜感激。

使用

pin = (EditText)v.findViewById(R.id.insert_pin);
input = pin.getText().toString();
要从insert_pin
EditText
获取文本,您需要使用对话框上下文

使用

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

要从insert_pin
EditText
中获取文本,您需要使用对话框上下文

您必须更改代码,因为您可以使用findviewbyd()查找EditText字段对象,但您应该使用对话框视图的findviewbyd()

按以下方式更改代码:

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

在使用findViewById()查找EditText字段对象时,您必须更改代码,但对于对话框视图,您应该使用findViewById()

按以下方式更改代码:

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

像这样编写代码

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

像这样编写代码

    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();
使用以下命令:

pin = (EditText)dialog.findViewById(R.id.insert_pin);
input = pin.getText().toString();
使用以下命令:

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

R.id.et1 EditText在哪里?R.id.et1 EditText在哪里?它表示类型对话框界面的“findViewById”方法未定义,并提示我添加强制转换;我这样做了,它说类型DialogInterface的方法“findViewById”未定义,并提示我添加强制转换;我这样做了,它说类型DialogInterface的方法“findViewById”未定义,并提示我添加强制转换;我是这样做的crashes@Peter我已经更新了我的代码,检查它是否正常。它表示类型DialogInterface的方法“findViewById”未定义,并提示我添加强制转换;我是这样做的crashes@Peter我已经更新了我的代码,检查它这应该工作完美