Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

popup - Can android PopupWindow show another PopupWindow?

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...