调试如下Android代码,弹出窗口

发布于 2024-12-26 00:49:51 字数 1356 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找回味觉 2025-01-02 00:49:51

弹出窗口需要一个初始窗口才能弹出。人们可能会认为 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

青衫负雪 2025-01-02 00:49:51

您确定布局文件中存在“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文