18

I want show a dialog box after specific condition , but for demo right now I want show a Dialog Box from the class which extends Application . here is my code

public class ControlApplication extends Application
{
    @Override
    {
    super.onCreate();
    final Dialog dialog = new Dialog ( getApplicationContext() ); 
    dialog.setTitle("zakasssssssssssssssssss");
    dialog.setCancelable(false);
    dialog.show();
}

}

but at the dialog.show() I am getting error like

Attempted to add window with non-application token WindowToken{4067a268 token=null}.  Aborting.
D/AndroidRuntime( 1923): Shutting down VM
W/dalvikvm( 1923): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 1923): FATAL EXCEPTION: main
E/AndroidRuntime( 1923): java.lang.RuntimeException: Unable to create application 
com.test.shrenik.ControlApplication: android.view.WindowManager$BadTokenException: 
Unable to add window -- token null is not for an application
E/AndroidRuntime( 1923):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3275)
E/AndroidRuntime( 1923):    at android.app.ActivityThread.access$2200(ActivityThread.java:117)
E/AndroidRuntime( 1923):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
E/AndroidRuntime( 1923):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1923):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1923):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 1923):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1923):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1923):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 1923):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 1923):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1923): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 1923):    at android.view.ViewRoot.setView(ViewRoot.java:531)
E/AndroidRuntime( 1923):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime( 1923):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime( 1923):    at android.app.Dialog.show(Dialog.java:241)
E/AndroidRuntime( 1923):    at com.andromeda.ui.pandora.ControlApplication.onCreate(ControlApplication.java:38)
E/AndroidRuntime( 1923):    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
E/AndroidRuntime( 1923):    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:969)
E/AndroidRuntime( 1923):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3272)
E/AndroidRuntime( 1923):    ... 10 more

can anybody suggest any solution ?

Guillaume
  • 22,694
  • 14
  • 56
  • 70
shankey
  • 333
  • 2
  • 7
  • 18
  • why don't you call dialog.show() from the activity which you want to the dialog will be show in? – Leo Dec 20 '11 at 15:45
  • I have many activities and after specific condition I want to show that Dialog box so it will not make sense to write the same code in each activity.. – shankey Dec 20 '11 at 15:59
  • so you can make your "dialog" variable to be a property of your "ControlApplication" class, then write a getter for it. Then you can make some conditions in "ControlApplication" which affect on "dialog", then when every activity, call something like: ((Application)getApplicationContext()).getDialog().show(). Hope it help :) – Leo Dec 20 '11 at 16:06
  • @LeoLink That won't work. You can't create a dialog using an Application context. – ahodder Dec 20 '11 at 16:12
  • ahh, i forgot about it, but why do you want to show a dialog at the time which onCreate of the activity's application is called?. If you don't want write a code snippet many times you can make a function and pass parameters to it. – Leo Dec 20 '11 at 16:16
  • @LeoLink Or create a class ;) – ahodder Dec 20 '11 at 16:21
  • Make a super class and in the onCreate you can show the dialog box if needed. This gets called automatically for all activities that extends this. Very basic and very Java. – slott Mar 07 '14 at 08:48

3 Answers3

9

Your program may behave as you want!**

** Just remember that you need to think about the consequences of its actions.

public class MyApplication extends Application {
        /** 
        * show example alertdialog on context -method could be moved to other class 
        * (eg. MyClass) or marked as static & used by MyClas.showAlertDialog(Context)
        * context is obtained via getApplicationContext() 
        */
        public void showAlertDialog(Context context) {
            /** define onClickListener for dialog */
            DialogInterface.OnClickListener listener 
                  = new   DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                // do some stuff eg: context.onCreate(super)
                }
            };

            /** create builder for dialog */
            AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setCancelable(false)
                .setMessage("Messag...")
                .setTitle("Title")
                .setPositiveButton("OK", listener);
            /** create dialog & set builder on it */
            Dialog dialog = builder.create();
            /** this required special permission but u can use aplication context */ 
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            /** show dialog */
            dialog.show();
        }

        @Override
        public void onCreate() {
            showAlertDialog(getApplicationContext());
        }
}

imports for abowe:

import android.app.AlertDialog;
import android.app.Application;
import android.app.Dialog; 
import android.content.Context;
import android.content.DialogInterface; 
import android.view.WindowManager;

edity:

You cannot **display an application window/dialog through a Context that is not an Activity or Service. Try passing a valid activity reference

** u can use application context to create dialog by adding before call to Dialog.show();

Dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 

- but this requires permission:  

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Ref:

ceph3us
  • 7,326
  • 3
  • 36
  • 43
2

You can't use an application [or service] context. If you really want to show you diallog from an application, you will have to pass an Activity context to it. You could also store the Activity context, but I do not recommend that. The activity context is voided on finish, so you will break your program. As @LeoLink said, just call it directly from your Activity.

EDIT For Example

class MyDialog {
    public Dialog show(Context context) {
        Dialog d = new Dialog(context);
        d.setTitle("I'm a dialog");
        d.setMessage("I'm a message");
        return d.show();
    }
}
ahodder
  • 11,353
  • 14
  • 71
  • 114
  • I have many activities and after specific condition I want to show that Dialog box, how to get Activity context and pass it to ControlApplication class ? – shankey Dec 20 '11 at 16:01
  • If you're hell bent on doing it, and again I do not recommend this, then place an Activity variable in you application. Then calling `Context.getApplicationContext()` and casting it to your specific application and calling a method to pass your activity context in through that method. Just like a normal setter. If you do this however, please, please, please don't rely on it and don't forget that you have to change it EVERY time the activity changes or it won't work. – ahodder Dec 20 '11 at 16:06
1

The Application class is there to hold data that can be used by your activities, not to interact with the UI. Display the dialog from the activity you want it displayed in.

EDIT: If you want to call the code from multiple activities, you can have a superclass for these activities, that contain the code with the dialog. Then extend this superclass in all the activities you want to display the dialog, and call it from there.

Guillaume
  • 22,694
  • 14
  • 56
  • 70
  • 2
    but from the same class I can display Toast , then why I cant show Dialog ? – shankey Dec 20 '11 at 15:57
  • I have many activities and after specific condition I want to show that Dialog box so it will not make sense to write the same code in each activity. – shankey Dec 20 '11 at 16:01
  • Toast is more of a device feature not an application feature. – ahodder Dec 20 '11 at 16:08
  • @shankey You don't have to write the same code. refer to my update – ahodder Dec 20 '11 at 16:09
  • If you really want to have that code in your Application (I still maintain it's bad practice), then you should pass the activity to the method. Then use the activity as the context for your dialog, it will work. But what I would have instead is a superclass for all your activities, with specific code (including your dialog code) – Guillaume Dec 20 '11 at 16:43