当我的应用程序运行时,我收到这个错误:"android.view.WindowManager$BadTokenException: Unable to add window -- token null无效;你的activity正在运行吗?“
我的代码是:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.nextround_popup, parent, false);
final PopupWindow popupWindow = new PopupWindow(popupView,(int) (width * .6), (int) (height * .8));
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.black_gradient));
new Handler().postDelayed(new Runnable(){
public void run() {
popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
}
}, 100L); 我已经找过了,我找到了一些解决方案,但我没有设法解决它。如果有任何帮助,我们将不胜感激,因为这只是一个框架:)
在进阶时谢谢
发布于 2015-05-15 10:44:29
此错误的原因是PopupWindow显示需要一个令牌,但视图在活动呈现完成之前不会拥有令牌。即使你把它推迟了。以及100ms是短的,但如果把它变长,它看起来很愚蠢。您可以在方法onWindowFocusChanged中显示PopupWindow,当Activity render完成时,此方法将被调用。如下所示:
class MyActivity extends Activity{
//...
@Override
public void onWindowFocusChanged(boolean hasFocus){
if(hasFocus){
//show your PopupWindow
popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
}
}
//...
}https://stackoverflow.com/questions/26444213
复制相似问题