Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试在空对象引用上调用虚拟方法android.widget.EditText.getText()_Android_Android Edittext_Android Alertdialog - Fatal编程技术网

尝试在空对象引用上调用虚拟方法android.widget.EditText.getText()

尝试在空对象引用上调用虚拟方法android.widget.EditText.getText(),android,android-edittext,android-alertdialog,Android,Android Edittext,Android Alertdialog,试图从对话框内的EditText获取Edit Text的值,但一次又一次地出现此错误 尝试调用虚拟方法“android.text.Editable” 空对象引用上的android.widget.EditText.getText() 我在另一个对话框上放大视图,它可以正常工作,但是每当我在对话框中单击ok时,我总是得到上面的错误 LayoutInflater li = LayoutInflater.from(getContext()); View promptsVi

试图从对话框内的EditText获取Edit Text的值,但一次又一次地出现此错误

尝试调用虚拟方法“android.text.Editable” 空对象引用上的android.widget.EditText.getText()

我在另一个对话框上放大视图,它可以正常工作,但是每当我在对话框中单击ok时,我总是得到上面的错误

LayoutInflater li = LayoutInflater.from(getContext());
                View promptsView = li.inflate(R.layout.custom_dimension_dialog, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getContext());
                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);
                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK",
                                new OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        // get user input and set it to result
                                        // edit text
                                        widthEditText = findViewById(R.id.Width);
                                        heightEditText = findViewById(R.id.Height);
                                        String width = widthEditText.getText().toString();
                                        String height = heightEditText.getText().toString();
                                        Toast.makeText(mContext, "Values are: " + width + " " + height, Toast.LENGTH_SHORT).show();
//                                        WIDTH=width
//                                        HEIGHT=height;
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                }).show(); 

您需要从对话框视图中查找EditText,如

widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);

您没有向edittext提供任何视图,请以这种方式更改代码

 widthEditText = promptsView.findViewById(R.id.Width);                                 
 heightEditText = promptsView.findViewById(R.id.Height);

您需要以这种方式使用视图
promptView.findviewbyId

@Umar检查我的答案它会work@MilanPansuriya再次感谢你,伙计@奥马尔接受我的回答
widthEditText = promptsView.findViewById(R.id.Width);
heightEditText = promptsView.findViewById(R.id.Height);