Context of the android dialog box
Last Update:2015-06-21
Source: Internet
Author: User
<span id="Label3"></p><pre><pre>The code is like This:</pre></pre><pre><pre><span style="color: #0000ff;">New</span> <span style="color: #000000;">alertdialog.builder (getapplicationcontext (), r.style.myalertdialogstyle) . settitle (</span>"gentle"<span style="color: #000000;">) . Setmessage (</span>"don't know if you don't want to \ \" + "why my heart"<span style="color: #000000;">) . Setpositivebutton (</span>"ok",<span style="color: #0000ff;">null</span><span style="color: #000000;">) . Setnegativebutton (</span>"cancel",<span style="color: #0000ff;">null</span><span style="color: #000000;">) . show ();</span></pre></pre><p><p></p></p><p><p>This time an error has been made: unable to add Window--token null was not for an application</p></p><p>Android.view.windowmanager$badtokenexception:unable to add Window--token null was not for an application<br>At Android.view.ViewRootImpl.setView (viewrootimpl.java:576)<br>At Android.view.WindowManagerGlobal.addView (windowmanagerglobal.java:269)<br>At Android.view.WindowManagerImpl.addView (windowmanagerimpl.java:69)<br>At Android.app.Dialog.show (dialog.java:289)<br>At Android.app.alertdialog$builder.show (alertdialog.java:951)<br>At Cn.bvin.app.androidtest_asgit. Mainactivity$1.onclick (mainactivity.java:24)<br>At Android.view.View.performClick (view.java:4446)<br>At Android.view.view$performclick.run (view.java:18480)<br>At Android.os.Handler.handleCallback (handler.java:733)<br>At Android.os.Handler.dispatchMessage (handler.java:95)<br>At Android.os.Looper.loop (looper.java:136)<br>At Android.app.ActivityThread.main (activitythread.java:5294)<br>At Java.lang.reflect.Method.invokeNative (Native METHOD)<br>At Java.lang.reflect.Method.invoke (method.java:515)<br>At Com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:864)<br>At Com.android.internal.os.ZygoteInit.main (zygoteinit.java:680)<br>At Dalvik.system.NativeStart.main (Native METHOD)</p><p><p></p></p><p><p>Why is this, look at the source code:</p></p><pre> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Create A Dialog window that uses the default Dialog frame Style. * * </span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Context The context the Dialog is to run it. In particular, it * uses the window manager and theme in this context to * present I TS UI. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Dialog (context Context) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>(context, 0,<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Create A Dialog window that uses a custom Dialog Style. * * </span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">context the context in which the Dialog should Run. In particular, it * uses the Windows Manager and theme from this context to * present Its UI. * </span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Theme A style resource describing the theme to use for the * WINDOW. See <a href= "{@</span></span><span style="color: #808080;"><span style="color: #808080;">Docroot</span></span><span style="color: #008000;"><span style="color: #008000;">}guide/topics/resources/available-resources.html#stylesandthemes ">style * and Theme resources</a> for M Ore information about defining and using * styles. This theme are applied on top of the current theme in * <var>context</var>. If 0, The default dialog theme would be Used. </span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Dialog (context context,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Theme) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>(context, theme,<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); } Dialog (context context,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Theme<span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">Createcontextthemewrapper) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(createcontextthemewrapper) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(theme = = 0<span style="color: #000000;"><span style="color: #000000;">) {typedvalue Outvalue</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Typedvalue (); Context.gettheme (). resolveattribute (com.android.internal.r.attr.dialogtheme, outvalue,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); Theme</span></span>=<span style="color: #000000;"><span style="color: #000000;">outvalue.resourceid; } Mcontext</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Contextthemewrapper (context, theme); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{mcontext</span></span>=<span style="color: #000000;"><span style="color: #000000;">context; } Mwindowmanager</span></span>=<span style="color: #000000;"><span style="color: #000000;">(windowmanager) Context.getsystemservice (context.window_service); Window W</span></span>=<span style="color: #000000;"><span style="color: #000000;">Policymanager.makenewwindow (mcontext); Mwindow</span></span>=<span style="color: #000000;"><span style="color: #000000;">w; W.setcallback (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); W.setonwindowdismissedcallback (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); W.setwindowmanager (mwindowmanager,</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">); W.setgravity (gravity.center); Mlistenershandler</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Listenershandler (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); }</span></span></pre><p><p>See the above public construction method is overloaded under the default constructor method, three parameters, but we see the public constructor method of the last Createcontextthemewrapper parameter is true</p></p><pre><pre><span>Context context, int theme, Boolean<span> createcontextthemewrapper<br></span></span>So then Mcontext is creating a contextthemewrapper object, which is understood.<br><br>Service,application,broadcast are inherited contextwraper, only activity is inherited contextthemewrapper.<br>and Contextthemewrapper succession contextwraper,contextwraper inherit the Context.<br><br>So activity.this get the context is can create Contextthemewrapper Object.<br>Then look at the source code of Contextthemewrapper:</pre></pre><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">contextthemewrapper () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span>(<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Contextthemewrapper (Context base,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">themeres) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">(base); Mthemeresource</span></span>=<span style="color: #000000;"><span style="color: #000000;">themeres; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Attachbasecontext (Context Newbase) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Attachbasecontext (newbase); } ... @ Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>SetTheme (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Resid) {mthemeresource</span></span>=<span style="color: #000000;"><span style="color: #000000;">resid; Initializetheme (); }</span></span></pre><pre><pre>Contextthemewrapper has two constructors, one with no parameters, one with a context and a themeres, and a context created by the latter in the dialog Constructor.<br>of course, the Contextthemewrapper object can also be created with no construction method, as the Attachbasecontext method can pass the context in,</pre></pre><pre><pre>Settheme can set the Themeres in ...</pre></pre><pre><pre><br><br></pre></pre><p><p>Context of the android dialog box</p></p></span>