• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Android Custom Dialog -- a simple example

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you find out *why* the app crashed? There should be a stack trace in the logcat output that points to the precise location in the code.
 
Monica Marcus
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Ulf! The logcat points to the line nr. 22

dialog.show()

and it says:

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?


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)



What is line 33 in the MainActivity class?
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 not mContext = 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...)
 
Monica Marcus
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Steve. Very useful comments. Thank you! And the link to the documented bug is helpful, good to know.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic