java android.view.WindowManager$BadTokenException: 无法添加窗口 — 令牌 null 无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17787011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android.view.WindowManager$BadTokenException: Unable to add window — token null is not valid
提问by marjanbaz
I get this error whenever I try to start my window class. I am using separate class, and not just a method within my game class, cause I need to disable back button on that popup window. I am calling this class with a button. This code works fine if I use it within my game class, but not in separate class. Here is my code:
每当我尝试启动窗口类时,都会收到此错误。我使用的是单独的类,而不仅仅是游戏类中的一个方法,因为我需要禁用该弹出窗口上的后退按钮。我正在用一个按钮调用这个类。如果我在我的游戏类中使用此代码,但不能在单独的类中使用它,则此代码工作正常。这是我的代码:
public class Popup_pogresno extends Activity implements OnClickListener{
private PopupWindow pwindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater
= (LayoutInflater)Popup_pogresno.this
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
pwindow = new PopupWindow(popupView, 300, 170, true);
Button btnDismiss = (Button)popupView.findViewById(R.id.bPopupOK);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
pwindow.dismiss();
}});
pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onBackPressed() {
}
}
回答by Vikram
You are not calling setContentView(R.layout.myLayout)
in your onCreate(Bundle)
method. Call it right after super.onCreate(savedInstanceState);
.
你没有setContentView(R.layout.myLayout)
在你的onCreate(Bundle)
方法中调用。之后立即调用super.onCreate(savedInstanceState);
。
This is from the Activity resource page on Android developers site:
这是来自 Android 开发者网站上的活动资源页面:
There are two methods almost all subclasses of Activity will implement:
onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.
onPause() is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be committed (usually to the ContentProvider holding the data).
几乎所有 Activity 的子类都会实现两种方法:
onCreate(Bundle) 是您初始化活动的地方。最重要的是,在这里您通常会使用定义 UI 的布局资源调用 setContentView(int),并使用 findViewById(int) 检索该 UI 中您需要以编程方式与之交互的小部件。
onPause() 是处理用户离开活动的地方。最重要的是,此时应该提交用户所做的任何更改(通常提交给持有数据的 ContentProvider)。
Edit 1:
编辑1:
Replace:
代替:
pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
with:
和:
new Handler().postDelayed(new Runnable(){
public void run() {
pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
}
}, 100L);