我有以下创建警报对话框的代码,并向其中添加了两个编辑文本,但是一旦运行应用程序,将不会检索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/