bug_ _ android.view.WindowManager$BadTokenException: Unable to add window — token

Programmer algorithm practice must read, common Java API skills to share>>>

========4. A common mistake about Android: unable to add window — token is not valid

android.view.WindowManager$BadTokenException:Unabletoaddwindow–tokenandroid.os. BinderProxy@41791b20isnotvalid ; isyouractivityrunning?
atandroid.view.ViewRootImpl.setView(ViewRootImpl.java:546)
atandroid.view.WindowManagerImpl.addView(WindowManagerImpl.java:302)
atandroid.view.WindowManagerImpl.addView(WindowManagerImpl.java:216)
atandroid.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:141)
atandroid.view.Window$LocalWindowManager.addView(W indow.java:537)
atandroid.app.Dialog.show(Dialog.java:278)
atandroid.app.AlertDialog$Builder.show(AlertDialog.java:991)
atandroid.widget.TextView.onTouchEvent(TextView.java:8430)
atandroid.view.View.dispatchTouchEvent(View.java:5553)
atandroid.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2027)
atandroid.view.ViewGr oup.dispatchTransformedTouchEvent(ViewGroup.java:2027)
atandroid.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1762)
atcom.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
atcom.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1397)
atandroid.app.Activ ity.dispatchTouchEvent(Activity.java:2431)
atdalvik.system.NativeStart.main(NativeMethod)

1. Error analysis

We can also understand the reason from the error message. The root cause of this problem is that the view to which the dialog will pop up does not exist

2. Where can the problem be solved: when the interface is destroyed, it will pop up again; Or when the interface jumps, our view changes, the context attached to dialog changes, or the interface doesn’t run

In addition, many times we need to call a view class method through a non component class to pop up the dialog or toast. In this way, we need to provide a static context to create the dialog or toast

For example, we use a static class to pop up a dialog box in a view

AlertDialog.Builderbuilder=newAlertDialog.Builder(mContextNew);

Of course, not all static contexts can be used to create a dialog. For example, the context of * * app (). Getapplication (). Getapplicationcontext () can’t, because it doesn’t represent any activity or view.. In this way, the dialog cannot be added

This view is used to bind display data. In its construction method, we initialize a static variable mcontextnew as mcontext of this view. In this way, we can pop up the dialog box through a static class, just pass in the static context (mcontextnew)

However, if this static context is only initialized in the construction method, there will be problems, because if another interface is created, and the view of binding data is also used, then the static context will be modified again

Therefore, when the new interface is finished, it returns to the previous interface. The static context is the context of the view that has just been finished. Therefore, if you still pass in the static variable and pop up the dialog box through a static class, you will get the above error that you can’t find the window

Solution:

For errors in the tab page, you can use the context of its parent class to pop up the dialog; If the interface has been destroyed, you can only judge whether the interface exists and then pop it up

For the use of static context to pop-up dialog can be solved by circumvention, such as to avoid static context being modified.. But this may limit the function of our program

Therefore, we can solve this problem by updating the static context when binding data, so that we can ensure that the static context is the context of the current interface view in any view. There will be no window whose parent class cannot be found

=====3 android.view.windowmanager $badtokeneexception: cannot add window – null token is invalid; Run for your activity

This is mainly about the form leakage of an activity in your Android, which is what we often call memory leakage. Why does the form leak

This is mainly when you open a pop window, as shown in the figure. If it exits the activity without closing the popupwindow, this error will occur

Because there is a sequence here. You need to close the popupwindow first, and then close the activity. This must be true. Popupwindow (form) cannot exist independently

If you understand this, it’s easy for you to solve it. Just close it with (Miss) first. When your window needs to be closed, add this sentence:

(PopupWindow.dismiss;) Popupwindow is the name of your window in your project. It’s best to make a judgment to determine whether the form is empty. If it’s not empty, close it. Otherwise, if there is time and space to close it, there will be an error

=========2. Click on button to pop up a dialog box. As a result, a problem was encountered. Android.view.windowmanager $badtokeneexception: 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();
}

There is a mistake in this sentence. Because my original writing method is: Builder = new alertdialog. Builder (this. Getapplicationcontext())

Because the API is new alertdialog. Builder (context context); I don’t think there are any grammatical mistakes. Results we searched the Internet to get the context (this. Getapplicationcontext ()); The difference between this and this:

This, of course, refers to the context of the activity, while this. Getapplicationcontext () refers to the context of the whole application

For alertdialog, you need to rely on a view, which corresponds to activity

So why do they report errors?This involves a life cycle problem

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

alertdialog belongs to activity. When an activity is destroyed, it must also be 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 android.app.LocalActivityManager$ LocalActivityRecord@45f48c40 is not valid; is your activity running?

Today, I encountered a problem when I was writing Google Maps for Android. I added an overlay to the map, and then I wanted to click the overlay Icon to pop up the dialog box. After I finished writing, the test reported the above exception.

The code is as follows:

//This is a method in a class that inherits itemizeddoverlay.

@Override public boolean onTap(int i) {

// TODO Auto-generated method stub OverlayItem item = allMapOverlays.get(i);

AlertDialog.Builder dialog = new AlertDialog.Builder(context); dialog.setTitle(item.getTitle());

dialog.setMessage(item.getSnippet()); dialog.show(); return true; }

Check the data and say that the problem is context. It says that getapplicationcontext () cannot be used. The context obtained by this must be your_ The context obtained by activity. This

And I use the last one, or travel.

So I want to describe my actual running environment. My map is in tabactivity, so the current form manager should be owned by the upper level tabactivity, not my map activity. So I use the context obtained by getparent() to run successfully.

Cause: the error is in popupwindow.showatlocation (findviewbyid (r.id.main), gravity.bottom, 0,0); Popwindow must be attached to a view. In oncreate, the view has not been loaded yet. You must wait until all the life cycle functions of the activity have been executed. You need to load the attached view before you can execute popwindow.

Solution 1: the showatlocation() function can be changed as follows:

//Revised code

[java]

view plain

copy

findviewById(R.id.mView).post(newRunnable(){

@Override

publicvoidrun(){

popwindow.showAtLocation(mView,Gravity.CENTER,0,0);

}

});

Summary: popupwindow must be displayed in an event or a new thread must be opened to call it. You can’t directly display a popupwindow in the oncreate method, otherwise there will always be the above errors.

Similar Posts: