Reputation: 73
Every time I try to run this simple app, I get this error:
Unable to add window -- token null is not valid; is your activity running?
But if I change this line:
vdlgDialogToShow = new Dialog(this);
and add it to onCreate
instead, it works.
public class MainActivity extends AppCompatActivity {
private Button vbtnShowDialog;
private Dialog vdlgDialogToShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vbtnShowDialog = findViewById(R.id.btnShowDialog);
vbtnShowDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayValidateEmailPopup();
}
});
}
public void displayValidateEmailPopup(){
// Criando variável necessária para mostrar a Dialog
vdlgDialogToShow = new Dialog(getApplicationContext());
vdlgDialogToShow.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Construindo Dialog
vdlgDialogToShow.setContentView(R.layout.dialog);
ImageView ivCloseWarningPopup = vdlgDialogToShow.findViewById(R.id.ivWarningDialogCloseImage);
TextView vtvDialogTitleText = vdlgDialogToShow.findViewById(R.id.tvWarningDialogTitle);
TextView vtvDialogBodyText = vdlgDialogToShow.findViewById(R.id.tvWarningDialogBodyText);
ImageView vivDialogImage = vdlgDialogToShow.findViewById(R.id.ivWarningDialogImage);
Button vbtnEnviarEmailConfirmacao = vdlgDialogToShow.findViewById(R.id.btnWarningDialogButton);
// Populando os campos da Dialog
vtvDialogTitleText.setText(R.string.warning_dialog_email_nao_confirmado_title);
vtvDialogBodyText.setText(R.string.warning_dialog_email_nao_confirmado_body);
vivDialogImage.setImageResource(R.drawable.ic_mail_white_dialog_warning);
vbtnEnviarEmailConfirmacao.setText(R.string.enviar_email_button_text);
// Verificando se a imagem que representa o fechamento da Dialog foi clicada
ivCloseWarningPopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vdlgDialogToShow.dismiss();
}
});
// Verificando se o Botão de enviar email de confirmação foi clicado
vbtnEnviarEmailConfirmacao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Verificando se o dispositivo está conectado à rede
}
});
vdlgDialogToShow.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Exibindo Dialog
vdlgDialogToShow.show();
}
}
I just would like to know why. This is probably a rookie question. But any help would be appreciated.
Upvotes: 1
Views: 34
Reputation: 970
In my case, it works just by changing getApplicationContext()
to this
.
The reason for that getApplicationContext()
is used to refer to the entire application which is used for application level. A dialog is part of an activity and hence, you cannot use application level context to initialize dialog.
Happy coding
Upvotes: 1
Reputation: 6073
It happened because you are showing your popup too early before activity is created and the parameter this
is refereed to the context of the activity which is null before the creation of activity !
To avoid BadTokenException
, you need to defer showing the popup until onCreate()
methods is called which means activity window is displayed .
and that's why it works fine when you put the code in onCreate
Upvotes: 0