Dimitar Iliev
Dimitar Iliev

Reputation: 1

How to start AlertDialog regardless the context? Android

I`m doing little diagnostics project for fiscal printer in Android. So when printing , a problem may occurs (missing paper ect.). In that case i want to start an AlertDialog notifying that there is a problem and asking the user does he wants to continue printing.

I want to make an AlertDialog that shows infront regardless the activity that is currently being brought to front.

I have tried the usual way of starting AlertDialog using GetAplicationContext() method but it crashes badly.

Here is the stacktrace:

05-11 17:36:56.162: W/dalvikvm(5458): threadid=3: thread exiting with uncaught exception (group=0x4001e390)
05-11 17:36:56.162: E/AndroidRuntime(5458): Uncaught handler: thread main exiting due to uncaught exception
05-11 17:36:56.182: E/AndroidRuntime(5458): java.lang.NullPointerException
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at bg.barcodes.mobile.routes.java.BaseActivity.onCreateDialog(BaseActivity.java:21)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at bg.barcodes.mobile.routes.java.DatecsPrinter.Send(DatecsPrinter.java:319)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at bg.barcodes.mobile.routes.java.DatecsPrinter.sendText(DatecsPrinter.java:381)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at bg.barcodes.mobile.routes.java.StatusDatecsPrinter.doCommand(StatusDatecsPrinter.java:134)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at bg.barcodes.mobile.routes.java.StatusDatecsPrinter.access$0(StatusDatecsPrinter.java:118)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at bg.barcodes.mobile.routes.java.StatusDatecsPrinter$1.onClick(StatusDatecsPrinter.java:61)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.view.View.performClick(View.java:2364)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.view.View.onTouchEvent(View.java:4179)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.widget.TextView.onTouchEvent(TextView.java:6650)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.view.View.dispatchTouchEvent(View.java:3709)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1708)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.os.Looper.loop(Looper.java:123)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at java.lang.reflect.Method.invoke(Method.java:521)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-11 17:36:56.182: E/AndroidRuntime(5458):     at dalvik.system.NativeStart.main(Native Method)
05-11 17:36:56.192: I/dalvikvm(5458): threadid=7: reacting to signal 3
05-11 17:36:56.222: I/dalvikvm(5458): Wrote stack trace to '/data/anr/traces.txt'

Any ideas ?

Upvotes: 0

Views: 418

Answers (3)

lenik
lenik

Reputation: 23508

don't try to use getApplicationContext(), create MyApplication class, inherited from the Application, then inside that class do the following:

public class MyApplication extends Application {
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        .........
    }

    public static Context getContext() {
        return instance;
    }

After that, you may use MyApplication.getContext() anywhere if you need a context and don't have an Activity lying around.

Upvotes: 1

James Cross
James Cross

Reputation: 7859

There is no way to interrupt another activity when it is running. However, the sort of functionality that you are looking for would be satisfied through using notifications. This does not show a dialog in front of the running activity, but a user will still be notified when something goes wrong:

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Upvotes: 0

waqaslam
waqaslam

Reputation: 68177

An alert dialog is displayed on above of an Activity, that's why you need an activity's context to initialize a dialog object. So, technically its not possible to show such dialogs without having reference to activity.

Upvotes: 0

Related Questions