Android custom dialog force close

android, dialog

Solution

You need to use your `Activty` as the `Context` for the `Dialog` not the `Application`.

Android is looking for a view to display the Dialog in and the `Application` does not have one but your `Activity` will. The `Application` is an object which ties your app together and has a `Context` as it knows about which locale you're running in and so on, but it relies on Activities to display things to the user.

Replace:

dialog = new Dialog(getApplicationContext());

with:

dialog = new Dialog(this);

Problem

I am implementing a custom dialog to form an about screen with a simple look and feel (which may become more complex once I get it working). I have a xml layout that loads some text and an icon from R. However when ever showDialog(int) is called I get a force close. I followed the instructions as per the dev guide: http://developer.android.com/intl/de/guide/topics/ui/dialogs.html code is as follows: ``` @Override protected Dialog onCreateDialog(int id) { Dialog dialog; switch(id) { case DIAG_ABOUT: dialog = new Dialog(getApplicationContext()); dialog.setContentView(R.layout.aboutdialog); dialog.setTitle(R.string.about_title); dialog.setOwnerActivity(this); break; default: dialog = null; } return dialog; } ``` DIAG_ABOUT is defined as `private static final int DIAG_ABOUT = 0;` The Log cat shows the following trace: ``` 03-04 11:37:08.760: ERROR/AndroidRuntime(726): Uncaught handler: thread main exiting due to uncaught exception 03-04 11:37:08.780: ERROR/AndroidRuntime(726): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.view.ViewRoot.setView(ViewRoot.java:429) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:178) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.app.Dialog.show(Dialog.java:231) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.app.Activity.showDialog(Activity.java:2407) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at uk.me.cpearson.runningLate.Home.onOptionsItemSelected(Home.java:143) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.app.Activity.onMenuItemSelected(Activity.java:2085) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:820) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:813) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:519) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.view.View.onTouchEvent(View.java:3828) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.widget.TextView.onTouchEvent(TextView.java:6291) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.view.View.dispatchTouchEvent(View.java:3368) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.view.ViewRoot.handleMessage(ViewRoot.java:1525) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.os.Handler.dispatchMessage(Handler.java:99) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.os.Looper.loop(Looper.java:123) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at android.app.ActivityThread.main(ActivityThread.java:3948) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at java.lang.reflect.Method.invokeNative(Native Method) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at java.lang.reflect.Method.invoke(Method.java:521) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 03-04 11:37:08.780: ERROR/AndroidRuntime(726): at dalvik.system.NativeStart.main(Native Method) ``` Can some one point out where I have gone wrong, I am using SDK 1.5

Original source