当很少有参数不满足时,我试图从 Retrofit 拦截器显示一个对话框。但是android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?在尝试显示对话框时出现异常。
这是我的代码。
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(ShieldSquare.applicationContext)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure to Exit")
.setMessage("Exiting will call finish() method")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
//set negative button
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what should happen when negative button is clicked
Toast.makeText(ShieldSquare.applicationContext,
"Nothing Happened", Toast.LENGTH_LONG).show(); …Run Code Online (Sandbox Code Playgroud)