我有一个 PopUpWindow,它需要在 Activity 启动后立即加载,它使用屏幕的 DisplayMetrics 以及其他东西。但是,我收到错误“无法添加窗口 - 令牌 null 无效;您的活动是否正在运行?” 为行welcome.showAtLocation(popupview2, Gravity.CENTER, 0, 0);
。
如何仅在检索资源后才显示 PopUpWindow?
我的代码:
@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);
}
所有帮助表示赞赏。