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

前端 未结 28 1407
死守一世寂寞
死守一世寂寞 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:00

    Here is how I resolved same error for my application:
    Adding the following line after creating the dialog:

    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);  
    

    You will not need to acquire a context. This is particularly useful if you are popping up another dialog over current popped up dialog. Or when it's not convenient to get a context.

    Hope this can help you with your app development.

    David

提交回复
热议问题