调试如下Android代码,弹出窗口
public class Offer_Popup extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offer_popup);
//newly added code, the window popup
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.popup_example, null, false),
100,
100,
true);
pw.showAtLocation(this.findViewById(R.id.relativeLayout_popup), Gravity.CENTER, 0, 0);}
/////////////////////
popup_example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Test Pop-Up"
/>
</LinearLayout>
DDMS 显示最后一行 showAtLocation 是错误。
它说无法添加窗口--令牌 null 无效。 android.view.WindowManagerBadToken。 在添加弹出窗口之前,程序运行良好。但是弹出代码后就崩溃了。它所说的坏标记是什么?
public class Offer_Popup extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.offer_popup);
//newly added code, the window popup
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.popup_example, null, false),
100,
100,
true);
pw.showAtLocation(this.findViewById(R.id.relativeLayout_popup), Gravity.CENTER, 0, 0);}
/////////////////////
popup_example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Test Pop-Up"
/>
</LinearLayout>
DDMS shows the last line showAtLocation is the error.
It says Unable to add window--token null is invalid. android.view.WindowManagerBadToken.
Before adding the popup, the program runs fine. But after the popup code, it crashed. what's the bad token it's talking about?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
弹出窗口需要一个初始窗口才能弹出。人们可能会认为 Activity 会自动拥有一个 Window,但事实并非如此。给定的 Activity 在
onAttachedToWindow()
之前没有活动窗口。此时,您可以将 PopupWindow 添加到 Window 中,因为存在 Window。如果您将代码移至 onAttachedToWindow(),它应该按预期工作,或者至少会给您一个完全不同的错误。
希望这有帮助,
模糊逻辑
A Popup Window requires an initial Window to be able to pop up from. One would think that an Activity automatically has a Window, but this is not the case right from the start. A given Activity does not have an active Window until
onAttachedToWindow()
. At this time, you may add the PopupWindow to the Window becaue there is a Window present.If you move the code to the onAttachedToWindow(), it should work as intended or at least give you a different error entirely.
Hope this helps,
FuzzicalLogic
您确定布局文件中存在“popup_example”并且定义正确吗?
请仔细检查该文件。并在“膨胀” pw 后仔细检查它的值。
Are you sure "popup_example" exists, and is defined correctly, in your layout file?
Please double-check the file. And double-check the value of pw after you "inflate" it.