Unable to add window-Token null is not for an application error Solution

Source: Internet
Author: User

Make a small one todayProgramWhen you add a view to windowmanager, an error is always reported:

Caused by: Android. View. windowmanager $ badtokenexception: unable to add window -- token null is not for an application

This error indicates that the token is empty, so the view cannot be added. I checked the information online and they said there was a problem with the passed context object, however, I have repeatedly tested that it is normal to start widnow in the activity and add a view. Isn't it possible to start windowmanager and add a view in initiating a broadcast?

 

If an error occurs when adding a view, it indicates windowmanager. what parameters should be missing in layoutparams? As a result, I checked it online. It is really necessary in windowmanager. some types are added to layoutparams, and the result is self-tested. The result is OK. The specificCodeAs follows:

 

 

/*** @ Author spring sky * Email vipa1888@163.com * my name: shi mingzheng * QQ 840950105 * @ Param height * @ Param width * @ return */private layoutparams getlayoutparam (INT height, int width) {If (wmparam = NULL) {// windowmanager. add type and flag to layoutparams so that the error wmparam = new windowmanager will not occur if the token is null is sent. layoutparams (-1, 4); wmparam. windowanimations = 0; wmparam. format = pixelformat. translucent | windowmanager. layoutparams. first_system_window;} wmparam. height = height; wmparam. width = width; return wmparam ;}

 

 

In this way, you can add a view to windowmanager. Another option is to set the pop-up box, and you need to input the context of activity. This!

 

 

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.