[英]AlertDialog not displaying in android studio
我在android studio中有一个AlertDialog
,如下所示:
final EditText columninput = new EditText(this);
columninput.setInputType(InputType.TYPE_CLASS_NUMBER);
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Choose column number");
adb.setView(columninput);
adb.setMessage("Please specify the number of columns you would like to decrypt your message with.");
adb.setCancelable(true);
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean bool = true;
try {
col = Integer.parseInt(columninput.getText().toString());
}
catch (Exception e) {
bool = false;
Toast t = Toast.makeText(getApplicationContext(), "Incorrect input", Toast.LENGTH_LONG);
t.show();
}
if (bool) {
dialog.dismiss();
}
}
});
adb.show();
method1(toRead, col);
注意, toRead
是在此上下文之外处理的另一个变量。 它不应引起任何错误。 当我运行我的应用程序时,从不显示AlertDialog
,并且method1
立即运行。 但是,当我注释掉最后一行( method1(toRead, col)
)时,运行应用程序时会出现alertdialog。 为什么会这样,如何确保始终显示alertDialog?
出现此问题的原因是,当您打开AlertDialog时 ,活动进入暂停状态。 因此,当您隐藏最后一行而不调用该方法时,活动将进入暂停状态,并且AlertDialog将成功显示。
在另一种情况下,当调用最后一个方法时,会发生以下情况:
在创建AlertDialog之前,将调用method1() 。
我希望这有帮助。
可以帮助您:
例:
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Delete")
.setMessage("Are you sure you want to delete Product?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dbni.delete(ItemID);
arrayStudent = dbni.selectAll();
list_view.setAdapter(new Adapter_viewitem(mactivity, arrayStudent));
Toast.makeText(getApplicationContext(), "Product deleted successfully", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("No", null).show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.