Android Error: Windowmanager$badtokenexception:unable to add window – window has already been added

Source: Internet
Author: User

Tag: CTI Manager flags application for OOP view RGBA logs RAS

Long before the test passed the code, now the phone upgraded Android7.0 after a run on the crash, reported such errors, the specific error is as follows:

Process:com.example.sho.android_anti_theft, pid:26807
Android.view. windowmanager$badtokenexception: Unable to add window-window [email protected] have already been added at Android.view.ViewRootImpl.setView (view rootimpl.java:691) at Android.view.WindowManagerGlobal.addView (windowmanagerglobal.java:342) at Android.view.WindowManagerImpl.addView (windowmanagerimpl.java:94) at Android.widget.toast$tn.handleshow ( toast.java:434) at Android.widget.toast$tn$2.handlemessage (toast.java:345) at Android.os.Handler.dispatchMessage ( handler.java:102) at Android.os.Looper.loop (looper.java:154) at Android.app.ActivityThread.main ( activitythread.java:6119) at Java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.zygoteinit$ Methodandargscaller.run (zygoteinit.java:886) at Com.android.internal.os.ZygoteInit.main (zygoteinit.java:776)

From the error message, I came to the conclusion that:

Before Android6.0, using WindowManager to add a suspended window, the type of suspension I used was type_toast, i.e.
Params.type = Windowmanager.layoutparams. Type_toast;
WindowManager. AddView (Contentview, params);
However, after the system upgrade Android7.0, it will be reported ndroid.view.windowmanager$badtokenexception:unable to add Window-window [e-mail protected] has Already been added
The mistake, specifically means that toast has been added to the window, so this time we can change the type_toast to Type_phone, so that you can display the suspended window, the program will not crash.

Here is the code for adding the suspended window, for reference only:

WindowManager WindowManager = (windowmanager) getsystemservice (Window_service); Windowmanager.layoutparams params = new     windowmanager.layoutparams ();p Arams.type = Windowmanager.layoutparams.type_phone;params.format = pixelformat.rgba_8888;params.gravity = Gravity.LEFT | Gravity.top;params.flags = Windowmanager.layoutparams.flag_not_focusable;params.width = Windowmanager.getdefaultdisplay (). GetWidth ();p arams.height = Windowmanager.getdefaultdisplay (). GetHeight (); params.x = 0;params.y = 0;windowmanager.addview (Contentview, params);      //Contentview need to define their own, and the addition of suspended windows need to dynamically apply for suspended window permissions. 

Android Error: Windowmanager$badtokenexception:unable to add window – window has already been added

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.