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

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

    Try to use the context of an activity which will be under the dialog. But be carefull when you use "this" keyword, because it will not work everytime.

    Forexample, if you have TabActivity as host with two tabs, and each tab is another activity, and if you try to create dialog from one of the tabs (activities) and if you use "this", then you will get exception, In this case dialog should be connected to host activity which host everything and visible. (you can say most visible parent Activity's context)

    I did not find this info from any document but by trying. This is my solution without strong background, If anybody with better knownledge, feel free to comment.

提交回复
热议问题