Fatal Exception: android.view.WindowManager$BadTokenException Unable to add window — token is not valid; is your activity running?

核能气质少年 提交于 2019-12-05 08:20:40

This problem is not because of Dialog but with Toast. Mainly this Toast issue is in android 7.1 version where the context become null. There is a solution created by this gentleman Solution . I have tested and this is working very well. You can try this.

 if (android.os.Build.VERSION.SDK_INT == 25) {
                ToastCompat.makeText(context, message, Toast.LENGTH_SHORT)
                        .setBadTokenListener(toast -> {
                            Log.e("failed toast", message);
                        }).show();
            } else {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }

Use Asynctask or Handler like:

Use private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message message) {
switch (message.what) {
  case DISPLAY_DLG:
    if (!isFinishing()) {
    dialogDownload.show();
    }
    break;
  }
 }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!