Forums Register Login

Android Custom Dialog -- a simple example

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?


+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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...)
+Pie Number of slices to send: Send
Hi, Steve. Very useful comments. Thank you! And the link to the documented bug is helpful, good to know.
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5966 times.
Similar Threads
How to display Images stored on sdcard?
how we can add two different lists with separator between the two???
Unable to get Bitmap image
AlertDialog changing look of font (size, colour, align)
How to add text to gridview
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:45:49.