I have added following code to display a Popover on my spinner view. The code uses custom PopOver which extends PopupWindow :
@Override
public void onStart() {
super.onStart();
new Handler().postDelayed(new Runnable() {
public void run() {
if (mActivity == null || !isAdded() || mActivity.isFinishing() || mSpinner == null) {
return;
}
displayPopOver();
}
}, 500L); // have seen BadTokenException if we try to show the popup too early
}
private void displayPopOver() {
//PopOver is a custom class that extends PopupWindow
PopOver p = PopOver.createVerticalPopOver(mActivity, PopOver.Type.TIP);
p.show(mSpinner);
}
I cannot reproduce the crash, but through crash reports I see the application crashes multiple times. What am I doing wrong? Can calling this code in onResume() instead of onStart() help?
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? 1 at android.view.ViewRootImpl.setView(ViewRootImpl.java:532) 2 at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259) 3 at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 4 at android.widget.PopupWindow.invokePopup(PopupWindow.java:1019) 5 at android.widget.PopupWindow.showAtLocation(PopupWindow.java:850) 6 at android.widget.PopupWindow.showAtLocation(PopupWindow.java:814) 7 at com.PopOver.show(PopOver.java:339) 8 at com.MainFragment.displayPopOver(MainFragment.java:515) 9 at com.MainFragment.access$1300(MainFragment.java:73) 10 at com.MainFragment$5.run(MainFragment.java:497) 11 at android.os.Handler.handleCallback(Handler.java:733) 12 at android.os.Handler.dispatchMessage(Handler.java:95) 13 at android.os.Looper.loop(Looper.java:136) 14 at android.app.ActivityThread.main(ActivityThread.java:5001) 15 at java.lang.reflect.Method.invokeNative(Native Method) 16 at java.lang.reflect.Method.invoke(Method.java:515) 17 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 18 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 19 at dalvik.system.NativeStart.main(Native Method)