android - 将 EditText 添加到警报对话框。

标签 android android-alertdialog

我有以下创建警报对话框的代码,我向其中添加了两个编辑文本,但是一旦我运行该应用程序,EditText 中的值将不会被检索并且我的应用程序因 NullPointerException 而崩溃:

代码是:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        LayoutInflater inflater=this.getLayoutInflater();
        final EditText usernameInput=(EditText)findViewById(R.id.dialogusername);
        final EditText passwordInput=(EditText)findViewById(R.id.dialogpassword);   
        alert.setView(inflater.inflate(R.layout.dialog,null));
        alert.setTitle("Enter Password");
        alert.setMessage("Enter password to remove the app:");
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            //provide user with caution before uninstalling
            //also here should be added a AsyncTask that going to read the password and once its checked the password is correct the app will be removed
            value1=usernameInput.getText().toString();
            value2=passwordInput.getText().toString();
            if(value1.equals(null)&&value2.equals(null))
            {Toast.makeText(context, "Enter username and password", Toast.LENGTH_SHORT).show();}
         }
        });
        });
        alert.show();

最佳答案

感谢大家对我问题的回答,我想我已经找到了我在上面发布的问题的解决方案:

AlertDialog.Builder alert = new AlertDialog.Builder(MyFeedActivity.this);
        LayoutInflater inflater=MyFeedActivity.this.getLayoutInflater();
        //this is what I did to added the layout to the alert dialog
        View layout=inflater.inflate(R.layout.dialog,null);       
        alert.setView(layout);
        final EditText usernameInput=(EditText)layout.findViewById(R.id.dialogusername);
        final EditText passwordInput=(EditText)layout.findViewById(R.id.dialogpassword);

我认为问题是我无法在警报对话框中获取 EidtText,但是通过上面的代码,一切正常。

关于android - 将 EditText 添加到警报对话框。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13584063/

相关文章:

java - Android 中可离线使用的文本转语音

Android:从谷歌地图获取无效的 KML 文件

android - 如何根据时间填充弹出窗口,然后在 android 中 20 秒后隐藏?

android - 保留 AlertDialog 选中的选项

android - 英雄动画上的 RenderFlex 溢出 - Flutter

android - 在我的 Android 应用程序中集成广告

Android NDK 编译 LAME HAVE_MPGLIB > 'interface.h' 找不到文件

android - 打开对话框时如何隐藏导航栏?

Android:正面、负面和中性按钮之间的区别

java - Android 警报对话框颜色已关闭