Bug_ _ android.view.windowmanager$badtokenexception:unable to add window-Token

Source: Internet
Author: User

android.view.windowmanager$badtokenexception:unable to add window-token [email protected] was not valid; Is your activity running?
At Android.view.ViewRootImpl.setView (viewrootimpl.java:546)
At Android.view.WindowManagerImpl.addView (windowmanagerimpl.java:302)
At Android.view.WindowManagerImpl.addView (windowmanagerimpl.java:216)
At Android.view.windowmanagerimpl$compatmodewrapper.addview (windowmanagerimpl.java:141)
At Android.view.window$localwindowmanager.addview (window.java:537)
At Android.app.Dialog.show (dialog.java:278)
At Android.app.alertdialog$builder.show (alertdialog.java:991)
At Android.widget.TextView.onTouchEvent (textview.java:8430)
At Android.view.View.dispatchTouchEvent (view.java:5553)
At Android.view.ViewGroup.dispatchTransformedTouchEvent (viewgroup.java:2027)
At Android.view.ViewGroup.dispatchTransformedTouchEvent (viewgroup.java:2027)
At Android.view.ViewGroup.dispatchTouchEvent (viewgroup.java:1762)
At Com.android.internal.policy.impl.phonewindow$decorview.superdispatchtouchevent (PhoneWindow.java:1953)
At Com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent (phonewindow.java:1397)
At Android.app.Activity.dispatchTouchEvent (activity.java:2431)
At Dalvik.system.NativeStart.main (Native Method)

1, Error Analysis:

From the error message we can also understand the reason, the root cause of this problem is because the dialog to be ejected will be attached to the view has not been caused.

2, where may be the problem: when the interface is destroyed and then bounced out, or the interface jumps when our view changes, dialog dependency context changes or the interface is not running.

In addition, many times we need to invoke a method of the view class through a non-component class to pop the dialog or toast, so we need to provide a static context to create this dialog or toast

For example, in a view, we have a static class that pops up a dialog box:

Alertdialog.builder Builder = new Alertdialog.builder (mcontextnew);

Of course not all static context can be used to create dialog, such as ***app (). Getapplication (). Getapplicationcontext () This context will not work, Because it doesn't represent which activity or view. This will not add the dialog.

This view is used to bind the display data, and we initialize a static variable mcontextnew The view's mcontext in its construction method. This allows us to pop up a dialog box with a static class, just pass in the static context (mcontextnew).

However, this static context is problematic if it is initialized only in the constructor method, because if a view of its bound data is used for another interface, the static context will be re-modified.

So when this new interface finishes and returns to the last interface, this static context is the context of the view that has just been finished. So if you still pass this static variable through a static class to pop up the dialog box, there will be no window error found above.

Workaround:

For the tab page error can be used in the context of its parent class to pop dialog, for the interface has been destroyed caused by the error can only determine whether the interface exists and then pop up;

For the use of static context to eject the dialog can be avoided by the way to solve, such as to avoid static context is modified. However, this may limit the function of our program.

So we can solve this problem by updating the static context at the time of bind data, so that this static context is the context of the view of the current interface in any view. It will not appear that the parent class window is not found.

===== 3 Android.view.WindowManager $ badtokenexception: Unable to add window-token null is invalid; Run for your campaign?

    1. The main point here is that you have an activity in your Android form leak, that is, we often say memory leaks, why the window experience leaks,

      The main one is when you open a Popupwindow (form). If it does not close the Popupwindow (form), it exits the activity and this error occurs.

      Because there is a sequence here, you have to close the Popupwindow, and then close the activity, this certain, Popupwindow (form) cannot exist independently.

    2. Understand this, you are easy to solve, you first use (dismiss) close on the line, in your form need to close, add this sentence:

      (Popupwindow.dismiss;) Popupwindow This is the name of your form in your project. It is better to make a judgment, determine whether the form is empty, if it is not empty, or if there is time and space is also closed error.

========= 2 button Click on a pop-up dialog box, the results encountered a problem, android.view.windowmanager$badtokenexception:unable to add window reported this error.

private void Showcustomdialog () {
Alertdialog.builder Builder;
Alertdialog Dialog;
Layoutinflater inflator = (layoutinflater) LayoutInflatorActivity.this.getSystemService (Layout_inflater_service);
View view = Inflator.inflate (r.layout.dialoglayout, NULL);
TextView Text = (TextView) View.findviewbyid (R.id.textview);
ImageButton ImageButton = (ImageButton) View.findviewbyid (R.id.imagebutton);
Builder = new Alertdialog.builder (this);
Builder.setview (view);
Dialog = Builder.create ();
Dialog.show ();
}

The report was wrong in this sentence. Because originally my writing is: Builder = new Alertdialog.builder (This.getapplicationcontext ());

Because it looks like the API is new Alertdialog.builder (context context), there is no grammatical error. Results Online Search, get Context this.getapplicationcontext ()); The difference between this and this:

This is of course the acitivity.this, referring to the context of this acitivity, and This.getapplicationcontext () refers to the context of the application as a whole.

For Alertdialog, there is a need to rely on a view, and the view corresponds to the activity.

So why is there an error, and here comes a life cycle problem.

For an application context, its life cycle is the life cycle of the entire application, and for activity, its life cycle ends when it is destroyed.


Alertdialog belongs to Acitivity, and it must be destroyed when the activity is destroyed, so here we specify the context of the activity.

==========1 android.view.windowmanager$badtokenexception:unable to add window

android.view.windowmanager$badtokenexception:unable to add window-token [email protected] was not valid; Is your activity running? I'm having a problem writing Google Maps for Android today, and I'm adding a overlay on the map and I want to play the dialog dialog when I click on the overlay icon, and after I've written this idea, The test reported the above exception. The code is as follows://This is a method in a class that inherits the itemizedoverlay .   @Override public boolean onTap (int i) {//TODO auto-generated method Stub Overlayitem item = Allmapoverlays.get (i); A Lertdialog.builder dialog = new Alertdialog.builder (context); Dialog.settitle (Item.gettitle ());d Ialog.setmessage (Item.getsnippet ()); Dialog.show (); return true; }   data said the problem lies in the context. Say you cannot use Getapplicationcontext (), this gets the context that must be obtained by your_activity.this. And I am using the last one, or on business. So I think of my actual operating environment, my map is in the tabactivity, then get the current form manager should be the first level tabactivity all, not my map activity so I use GetParent () to get the context, Run successfully.     Reason: Error in Popupwindow.showatlocation (Findviewbyid (R.id.main), gravity.bottom,0,0); Popwindow must be attached to a view, and in OnCreate the view has not been loaded, it must wait for the activity's life cycle function to complete execution, you need to attach the view loaded before you can perform popWindow

Workaround the 1:showatlocation () function can be changed like this:

Post-correction Code
[Java]View PlainCopy
    1. Findviewbyid (R.id.mview). Post (new Runnable () {
    2. @Override
    3. public Void Run () {
    4. Popwindow.showatlocation (MView, Gravity.center, 0, 0);
    5. }
    6. });

Summary: Popupwindow must appear in an event or open a new thread to invoke, not directly in the OnCreate method to display a popupwindow, otherwise there will always be the above error.

Bug_ _ android.view.windowmanager$badtokenexception:unable to add window-Token

Related Article

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.