I tried to do a simple example of Custom Dialog and initially I wrote it like this:
The result was that the application crashed right after launching. Then I thought that
Context mContext = null;
should be instead
Context mContext = this;
and also the assignment to mContext inside the method on line 11 becomes no longer necessary. I would be very grateful if someone could explain why is this so. It seems reasonable, though I can’t grasp the full meaning.
05-15 16:37:31.703: E/AndroidRuntime(845): FATAL EXCEPTION: main
05-15 16:37:31.703: E/AndroidRuntime(845): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dialogapp_customdialog1/com.example.dialogapp_customdialog1.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-15 16:37:31.703: E/AndroidRuntime(845): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.os.Looper.loop(Looper.java:137)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-15 16:37:31.703: E/AndroidRuntime(845): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 16:37:31.703: E/AndroidRuntime(845): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 16:37:31.703: E/AndroidRuntime(845): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-15 16:37:31.703: E/AndroidRuntime(845): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-15 16:37:31.703: E/AndroidRuntime(845): at dalvik.system.NativeStart.main(Native Method)
05-15 16:37:31.703: E/AndroidRuntime(845): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-15 16:37:31.703: E/AndroidRuntime(845): at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.app.Dialog.show(Dialog.java:281)
05-15 16:37:31.703: E/AndroidRuntime(845): at com.example.dialogapp_customdialog1.MainActivity.onCreate(MainActivity.java:33)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.app.Activity.performCreate(Activity.java:5104)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-15 16:37:31.703: E/AndroidRuntime(845): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-15 16:37:31.703: E/AndroidRuntime(845): ... 11 more
So the problem is "Unable to start activity. Unable to add window -- token null is not for an application" But why is "this" a good replacement for "null"? The "this" keyword refers to the current object, but later on (line 11) I did make a non-null assignment to mContext. It seems that the method getApplicationContext() returns null. Now I see that getApplicationContext() is a public abstract method of the abstract class Context. Oh, It seems that something is wrong here. Perhaps this is the explanation? But why is there no comply about using an abstract method?
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
...
05-15 16:37:31.703: E/AndroidRuntime(845): at com.example.dialogapp_customdialog1.MainActivity.onCreate(MainActivity.java:33)
Monica Marcus wrote:...But why is "this" a good replacement for "null"? The "this" keyword refers to the current object,
this refers to the instance of MainActivity, which extends Activity, which extends Context. So this is an instance of Context.
but later on (line 11) I did make a non-null assignment to mContext. It seems that the method getApplicationContext() returns null. Now I see that getApplicationContext() is a public abstract method of the abstract class Context. Oh, It seems that something is wrong here. Perhaps this is the explanation? But why is there no comply about using an abstract method?
The method that is being called is a non-abstract implementation of the method defined in some sub-class of Context. The Activity is a non-abstract subclass of Context so its getApplicationContext() must be non-abstract.
Monica Marcus wrote:Hi, Ulf! The logcat points to the line nr. 22
dialog.show()
Sorry, missed this with my first response...
It seems that the method getApplicationContext() returns null.
Okay, did a little research. getApplicationContext() can't return null, but it returns the Application. The Application instance is apparently not a suitable Context implementation for the Dialog, and you should be using the Activity instead. This means you should be using mContext = this; and notmContext = getApplicationContext();. So get rid of that line.
See: https://code.google.com/p/android/issues/detail?id=11199 The problem may be a code bug, but is at least a documentation bug that has spread pretty far. The latest docs say you should be using DialogFragments instead, anyway if possible (remember the DialogFragment class is available in the support library, which makes it quite backwards compatible...)