Unable to open Menu options programmatically without user click

In my project, I try to open optionsMenu programmatically without clicking on the menu button from the user. I tried openOptionsMenu () , but it gives me an error like this android.view.WindowManager $ BadTokenException: cannot add window - .. why is this ... what is the solution for this ...

08-13 14: 53: 02.687: E / AndroidRuntime (1213): FATAL EXCEPTION: main 08-13 14: 53: 02.687: E / AndroidRuntime (1213): java.lang.RuntimeException: Cannot start Activity ComponentInfo {com. veytila.candideye / com.veytila.candideye.MainActivity}: android.view.WindowManager $ BadTokenException: Unable to add window zero token; Does your activity work?

+5
source share
2 answers

I believe you are doing this in Oncreate or onResume. You will only need to call this api after the operation window has been initialized. One way is to post the delayed runnable in your onresume to the ui thread.

+1
source

thanks for ur replica ..... i found a solution from mr.Nandeesh post. I just added the following code and its work ...

 public void onResume()
{
    new Handler().postDelayed(new Runnable() { 
       public void run() { 
           openOptionsMenu(); 
       } 
    }, 500); 

    super.onResume();
}
+2
source

All Articles