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

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

    I was using ProgressDialog in a fragment and was getting this error on passing getActivity().getApplicationContext() as the constructor parameter. Changing it to getActivity().getBaseContext() didn't work either.

    The solution that worked for me was to pass getActivity(); i.e.

    progressDialog = new ProgressDialog(getActivity());

提交回复
热议问题