Causes analysis and solution of Popupwindow-induced anomalies in the OnCreate method of activity

Source: Internet
Author: User

First, preface
In some cases, we need to show popupwindow when we enter activity, such as a common selection interface. However, because Popupwindow is dependent on activity, if the activity has not been created, the activity has not been fully displayed to show Popupwindow, there will be abnormal phenomenon.

second, the problem is reproduced
I call the following method in the activity's OnCreate () method:
    public void Show () {        if (null! = Mpopupwindow) {            mpopupwindow.showatlocation (mView, gravity.center, 0, 0);        }    }

The following exception occurred while running the program:



Third, the solution
Search This question on StackOverflow you will find that there is no reason to analyze, but I found the answer in the book "Essentials of Android Development" (P158):

Popupwindow does not pop up from the fixed position of the screen like a dialog box, but relies on the location of the anchor control object, the so-called anchor control object, which is a control in the interface component, and the Popupwindow display and function are centered on it as the extension interface of the anchor control. To enhance the functionality of the control object.

Pop-ups are closely related to the stroke control, and you need to ensure that the control tree in which the anchor control is located is already connected to the Window Management service before you construct and present the pop-up window, because the Window object is required to obtain relevant information during the presentation of the popup. During the construction of the interface component, the establishment of the window connection is an asynchronous process, that is, when the activity.oncreate () function is called, the interface and the Window Management Service's two-way communication connection is not established, and if the pop-up window is constructed at this time, an exception will be thrown. Therefore, if you expect to construct a popup window at the point where the interface component is presented, you can also convert the popup object construct into an asynchronous process.

Call Final View anchor = Findviewbyid (R.id.anchor) in the interface component OnCreate function;    Anchor.post (New Runnable () {    @Override public    void Run () {        //constructs and presents pop-up windows        popupwindow window = CreateWindow ();        Window.showasdropdown (anchor);    }});

Before a connection is made to the Window Management Service, the interface component will put a message sent through the View.post function into a static queue, after the communication connection is established, and then read the message from that queue and execute it all. Thus, this implementation model ensures that the window communication connection has been built successfully when the popup window is displayed.

        So for the above problem, the simplest way to do this is to display the Popupwindow asynchronously:
    public void Show () {        mview.post (new Runnable () {            @Override public            void Run () {                if (null! = Mpopupwindow {                    mpopupwindow.showatlocation (mView, gravity.center, 0, 0);}                }        );    }

Four, off-topic

today want to do a video in the Popupwindow inside the function, the results found that surfaceview in Popupwindow is not normal display, if you need to display Surfaceview, it is recommended to use the view, Fragment or dialog activity instead of Popupwindow. I checked on the StackOverflow, said that Surfaceview must be attached to the window, but Popupwindow is attached to the view, so surfaceview in the Popupwindow can not be created normally, See also: Surfaceview not working inside Popupwindow


Display Popupwindow cause analysis and resolution of the exception in the OnCreate method of activity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.