可以PopupWindow的android显示另一个PopupW

可以PopupWindow的android显示另一个PopupW

本文介绍了可以PopupWindow的android显示另一个PopupWindow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以的android PopupWindow显示另一个PopupWindow?多少PopupWindow可以在同一时间打开?只有一个?

第一PopupWindow正常显示。但在按一下按钮(这是在第一PopupWindow内容查看),我有一个例外:

  08-13 16:28:38.682:ERROR / AndroidRuntime(11760):致命异常:主要
        android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.view.ViewRootImpl$W@41286250无效;在您的活动运行?
        在android.view.ViewRootImpl.setView(ViewRootImpl.java:600)
        在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:313)
        在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
        在android.view.WindowManagerImpl $ CompatModeWrapper.addView(WindowManagerImpl.java:140)
        在android.view.Window $ LocalWindowManager.addView(Window.java:537)
        在android.widget.PopupWindow.invokePopup(PopupWindow.java:992)
        在android.widget.PopupWindow.showAsDropDown(PopupWindow.java:901)
        在org.example.qberticus.quickactions.BetterPopupWindow.showLikePopDownMenu(BetterPopupWindow.java:159)
        在org.example.qberticus.quickactions.BetterPopupWindow.showLikePopDownMenu(BetterPopupWindow.java:129)
        在name.antonsmirnov.android.popup.ui.MainActivity $ 1 $ 1.运行(MainActivity.java:44)
        在android.app.Activity.runOnUiThread(Activity.java:4170)
        在name.antonsmirnov.android.popup.ui.MainActivity $ 1.onClick(MainActivity.java:42)
        在android.view.View.performClick(View.java:3558)
        在android.view.View $ PerformClick.run(View.java:14157)
        在android.os.Handler.handleCallback(Handler.java:605)
        在android.os.Handler.dispatchMessage(Handler.java:92)
        在android.os.Looper.loop(Looper.java:137)
        在android.app.ActivityThread.main(ActivityThread.java:4514)
        在java.lang.reflect.Method.invokeNative(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:511)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        在dalvik.system.NativeStart.main(本机方法)
 

在code是:

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    的setContentView(R.layout.main);
    bindControls();
    initControls();
}

私人Button按钮;

私人无效bindControls(){
    按钮=(按钮)findViewById(R.id.button);
}

私人无效initControls(){
    initButton(按钮);
}

私人无效initButton(最终Button按钮){
    button.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图查看){
            最后BetterPopupWindow窗口=新BetterPopupWindow(按钮);

            查看popupview = createPopupView();
            window.setContentView(popupview);
            runOnUiThread(新的Runnable(){
                公共无效的run(){
                    window.showLikePopDownMenu();
                }
            });
        }
    });
}

私人查看createPopupView(){
    视图V = LayoutInflater.from(MainActivity.this).inflate(R.layout.window,NULL);
    按钮弹出式按钮=(按钮)v.findViewById(R.id.popupbutton);
    initButton(弹出式按钮);
    返回伏;
}
 

解决方案

玩它之后我发现,

  window.showAtLocation(getWindow()getDecorView(),Gravity.CENTER,X,Y);
 

工作好,但

  window.showAsDropDown(getWindow()getDecorView(),Gravity.CENTER,X,Y);
 

引发例外!如果你使用 showAtLocation(视图) getWindow()不同的任何观点。getDecorView()你仍然有一个例外。

Can android PopupWindow show another PopupWindow?How many PopupWindow can be opened in the same time? Only one?

The first PopupWindow is displayed normally. But on button click (which is in the first PopupWindow contentview) i am having an exception:

08-13 16:28:38.682: ERROR/AndroidRuntime(11760): FATAL EXCEPTION: main
        android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@41286250 is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:600)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:313)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
        at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
        at android.view.Window$LocalWindowManager.addView(Window.java:537)
        at android.widget.PopupWindow.invokePopup(PopupWindow.java:992)
        at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:901)
        at org.example.qberticus.quickactions.BetterPopupWindow.showLikePopDownMenu(BetterPopupWindow.java:159)
        at org.example.qberticus.quickactions.BetterPopupWindow.showLikePopDownMenu(BetterPopupWindow.java:129)
        at name.antonsmirnov.android.popup.ui.MainActivity$1$1.run(MainActivity.java:44)
        at android.app.Activity.runOnUiThread(Activity.java:4170)
        at name.antonsmirnov.android.popup.ui.MainActivity$1.onClick(MainActivity.java:42)
        at android.view.View.performClick(View.java:3558)
        at android.view.View$PerformClick.run(View.java:14157)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4514)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)

the code is:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    bindControls();
    initControls();
}

private Button button;

private void bindControls() {
    button = (Button) findViewById(R.id.button);
}

private void initControls() {
    initButton(button);
}

private void initButton(final Button button) {
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            final BetterPopupWindow window = new BetterPopupWindow(button);

            View popupview = createPopupView();
            window.setContentView(popupview);
            runOnUiThread(new Runnable() {
                public void run() {
                    window.showLikePopDownMenu();
                }
            });
        }
    });
}

private View createPopupView() {
    View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.window, null);
    Button popupButton = (Button) v.findViewById(R.id.popupbutton);
    initButton(popupButton);
    return v;
}
解决方案

After playing with it i've found that

    window.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, x, y);

works okay, but

    window.showAsDropDown(getWindow().getDecorView(), Gravity.CENTER, x, y);

raises exception! If you use showAtLocation(view) with any view different from getWindow().getDecorView() you'll still have an exception.

这篇关于可以PopupWindow的android显示另一个PopupWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 20:47