gpt4 book ai didi

android - 在处理程序中创建警报对话框

转载 作者:太空狗 更新时间:2023-10-29 15:48:24 30 4
gpt4 key购买 nike

我想从一个处理程序创建一个警报对话框,当线程终止时它会收到信号这是我的代码导致的:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

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;
}
}


}
};

最佳答案

问题可能是 getApplicationContext() 不是您的 Activity 上下文。

    alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
//should be change to
alertDialog = new AlertDialog.Builder( YourActivity.this ).create();

关于android - 在处理程序中创建警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317456/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com