Yesterday I had a hard time worrying about my day's problem. Today I finally got a little eye-catching. It seems that it is necessary to take a rest in this line... Today, it seems that many errors of this type have been made ..
In the past few days, I am working as a client, and the company's product management is moved from the server to Android. This error persists during development:
Android. View. windowmanager $ badtokenexception: unable to add window -- token null is not for an appli
This is an error I made when I used tabhost for application and jumped between activty in the sub-tag. As we all know, we need to implement activity jump in the tabhost sub-tag, instead of overwriting tabhost. We need to use activitygroup. That is to say, activitygroup is required for our main activity. During the jump, we can use the following code.
// Pass the polling information of the current click to the next activitypollingrequestvo pollingrequestvo = listallvo. get (adapter. position); intent = new intent (pollingmanagementactivity. this, pollinginforactivity. class); // intent. setflags (intent. flag_activity_single_top); bundle = new bundle (); bundle. putserializable ("pollingrequestvo", pollingrequestvo); intent. putextras (bundle); intent. setflags (intent. flag_activity_clear_top); holder. viewswitcher. removeallviews (); view = getlocalactivitymanager (). startactivity ("polling", intent ). getdecorview (); holder. viewswitcher. addview (View );
Of course, this class pollingmanagementactivity inherits the mytabactivity class, And the mytabactivity class also inherits the activitygroup class and overwrites the onbackpress method of the activitygroup class, as follows:
@ Overridepublic void onbackpressed () {New alertdialog. Builder (this). settitle ("NOTE"). setmessage ("are you sure you want to exit the mobile network administrator? "). Seticon (Android. r. drawable. ic_menu_help ). setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface diich, int which) {dialog. dismiss (); finish ();}}). setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface diich, int which) {dialog. cancel ();}}). show ();}
This overwrites the note to better manage the activity. As long as the class inherits mytabactivity, this method is useful and the problem arises. Let's look at the above activity jump code, when we jump to pollinginforactivyt, we can directly click the "return" key to run the onbackpress method of the parent class below. However, if you click another tab, the following error occurs:
Android. View. windowmanager $ badtokenexception: unable to add window -- token null is not for an appli error.
I found many problems online and finally found them. This is because we use new alertdialog. Builder (this) When inheriting the activitygroup and overwriting the onbackpress method.
Why. The reason is that after we jump to the target activity, if this activyt still inherits mytabactivyt, new alertdialog. in Builder (this), this indicates the current activyt, namely, pollinginforactivyt. However, we can see that when we jump to activyt, our activity is pasted to holder in the form of view. viewswitcher, you cannot add view by yourself (the dialog box is also a view added by activyt), you must use pollingmanagementactivity to replace this, we only need to in pollinginforactivity, you can use getparent to replace this. In this case, the actual dialog box is added by pollingmanagementactivity .....