Pregunta

In my project i am trying to open the optionsMenu programmatically without a click on the menu key from the user. I have tried openOptionsMenu() but it gives me an error like this android.view.WindowManager$BadTokenException: Unable to add window -- .. why it is... 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: Unable to start activity ComponentInfo{com.veytila.candideye/com.veytila.candideye.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

¿Fue útil?

Solución

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

Otros consejos

thanks for ur replis..... i found solution from mr.Nandeesh's post. I just added the following code and its working...

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

    super.onResume();
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top