我想从处理程序中创建一个警报对话框,当线程终止时会发出信号,这是导致错误的代码:
请注意,这个错误可能是由于尝试在非活动上下文中创建对话框而引起的。android.view.WindowManager $ BadTokenException:无法添加窗口-- 令牌为空不是应用程序
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (dialog != null)
{
dialog.dismiss();
dialog = null;
}
switch (serverResponseCode)
{
case 200:
{
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
alertDialog.setTitle("Super :)");
alertDialog.setMessage("Poza a fost trimisa cu success.");
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
} });
alertDialog.show();
serverResponseCode = -1;
break;
}
default:
{
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
alertDialog.setTitle("Eroare :(");
alertDialog.setMessage("Eroare la trimiterea pozei.");
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
} });
alertDialog.show();
break;
}
}
}
};