0

I have a PopUpWindow which needs to load as soon as the Activity starts, which uses the DisplayMetrics of the screen, as well as other things. However, I am getting the error "Unable to add window -- token null is not valid; is your activity running?" for the line welcome.showAtLocation(popupview2, Gravity.CENTER, 0, 0);.

How can I have the PopUpWindow be shown only when the resources have been retrieved?

My code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

    dpWidth = displayMetrics.widthPixels / displayMetrics.density;

    dpHeight = displayMetrics.heightPixels / displayMetrics.density;

popupview2 = inflater.inflate(R.layout.start_layout, null);

welcome = new PopupWindow(popupview2, (int) dpWidth, (int) dpHeight);
    welcome.setAnimationStyle(R.style.Animation2);

    welcome.showAtLocation(popupview2, Gravity.CENTER, 0, 0);

}

All help appreciated.

user3658385
  • 101
  • 1
  • 1
  • 6
  • There're lots of answers: http://stackoverflow.com/questions/8782250/popupwindow-badtokenexception-unable-to-add-window-token-null-is-not-valid Just try to search "Unable to add window -- token null is not valid" – eleven Jun 19 '14 at 21:01
  • I considered this, but I can't delay it as I need the PopUpWindow to appear as soon as the app is loaded (i.e., the PopUpWindow needs to be on top of the main activity, instead of the main activity appearing, THEN the popupwindow appearing as it was delayed). – user3658385 Jun 19 '14 at 21:16

0 Answers0