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 PopUpWindow acts as a splash (so that it can fade out instantly onClick), so the window must be the first thing the user sees.
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);
}
Basically, is there any sort of "onResourcesLoaded"-like thing I can use? All help appreciated.
When your code is invoked the system is not ready to measure metrics
Move all your code into this method :
@Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
super.onAttachedToWindow();
}
This method will be invoked when all the metrics can be used because the activity is completely displayed.