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.