无论上下文如何启动AlertDialog? Android的

时间:2012-05-11 14:23:13

标签: android alertdialog

我在Android中为财务打印机做了很少的诊断项目。 因此,在打印时,可能会出现问题(缺少纸张等)。在这种情况下,我想启动AlertDialog通知存在问题,并询问用户是否要继续打印。

我想创建一个AlertDialog,无论当前被带到前面的活动如何都显示在前方。

我尝试过使用GetAplicationContext()方法启动AlertDialog的常用方法,但崩溃严重。

这是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'

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

不要尝试使用getApplicationContext(),创建MyApplication类,继承自Application,然后在该类中执行以下操作:

public class MyApplication extends Application {
    private static MyApplication instance;

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

    public static Context getContext() {
        return instance;
    }

之后,如果您需要上下文并且没有MyApplication.getContext(),那么您可以在任何地方使用Activity

答案 1 :(得分:0)

在Activity的上方显示警告对话框,这就是您需要活动的上下文来初始化对话框对象的原因。因此,从技术上讲,如果没有参考活动,就无法显示此类对话框。

答案 2 :(得分:0)

运行时无法中断其他活动。但是,通过使用通知可以满足您要查找的功能。这不会在运行活动前显示对话框,但在出现问题时仍会通知用户:

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