1

I am creating a project with one activity and 3 fragments on it with bottom navigation. in onAttach() function i get the context and work with it app was working properly but suddenly i got error mention below on opennig the custom dialogue on research i came to know it came due to activity is finished but how is that possible that activity is finished the fragment is running on that activity also context is not null getContext() is also returning context properly

to add window -- token null is not valid; is your activity running?

the only solution i found is to check for activity if its running i will show the dialogue other wise not

Dialog which is not opening is

MyAlertDialogue.getInstance(mContext)
        .setLayout(R.layout.dialogue_notify)
        .showTitle("Info")
        .showPositiveButton("Ok")
        .showMessage("Task Done!")
        .show();

That's What i am getting

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:953)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
        at android.app.Dialog.show(Dialog.java:329)
        at co.xeontechnologies.slapapp.utils.MyAlertDialogue.show(MyAlertDialogue.java:160)
        at co.xeontechnologies.slapapp.fragments.HomeFragment.btn_inzo(HomeFragment.java:234)
        at co.xeontechnologies.slapapp.fragments.HomeFragment_ViewBinding$1.doClick(HomeFragment_ViewBinding.java:45)
        at 

 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 Answers1

0

set getContext()

 MyAlertDialogue.getInstance(getContext())
            .setLayout(R.layout.dialogue_notify)
            .showTitle("Info")
            .showPositiveButton("Ok")
            .showMessage("Task Done!")
            .show();

or this may be helpfull https://stackoverflow.com/questions/7933206/android-unable-to-add-window-token-null-is-not-for-an-application-exception

YuvrajsinhJadeja
  • 1,383
  • 7
  • 23