如何从Android中扩展应用类显示对话框? [英] How to show Dialog Box from the class which extends Application in android?

查看:302
本文介绍了如何从Android中扩展应用类显示对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要展示后,特定条件下的一个对话框,但试玩现在我想从它扩展应用类显示一个对话框。 这里是我的code

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();
}

}

但在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 ?

推荐答案

您不能使用应用程序[或服务]上下文。如果你真的想告诉你从一个应用程序diallog,你将有一个活动范围内传递给它。您也可以存储在活动范围内,但我的不是 的建议。活动背景下是无效的光洁度,所以你会打破你的计划。正如@LeoLink说,只是直接从你的Activity调用它。

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.

编辑例如:

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();
    }
}

这篇关于如何从Android中扩展应用类显示对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆