Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

前端 未结 28 1676
死守一世寂寞
死守一世寂寞 2020-11-22 08:42

My Activity is trying to create an AlertDialog which requires a Context as a parameter. This works as expected if I use:

AlertDialog.Builder builder = new Al         


        
28条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 09:09

    For future readers, this should help:

    public void show() {
        if(mContext instanceof Activity) {
            Activity activity = (Activity) mContext;
            if (!activity.isFinishing() && !activity.isDestroyed()) {
                dialog.show();
            }
        }
    }
    

提交回复
热议问题