如何在android中扩展Application的类中显示对话框?
我想在特定条件后显示一个对话框,但对于现在的演示,我想显示扩展 Application 的类中的对话框。 这是我的代码
public class ControlApplication extends Application
{
@Override
{
super.onCreate();
final Dialog dialog = new Dialog ( getApplicationContext() );
dialog.setTitle("zakasssssssssssssssssss");
dialog.setCancelable(false);
dialog.show();
}
}
但是在dialog.show()中我收到错误,
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
有人可以建议任何解决方案吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的程序可能会按照您的意愿运行!**
** 请记住,您需要考虑其行为的后果。
导入 abowe:
编辑:
** 您可以通过添加 在调用 Dialog.show(); 之前使用应用程序上下文来创建对话框
参考:
Your program may behave as you want!**
** Just remember that you need to think about the consequences of its actions.
imports for abowe:
edity:
** u can use application context to create dialog by adding before call to Dialog.show();
Ref:
您不能使用应用程序[或服务]上下文。如果您确实想显示应用程序中的对话日志,则必须将活动上下文传递给它。您还可以存储 Activity 上下文,但我不建议这样做。活动上下文在完成时无效,因此您将破坏您的程序。正如@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.
EDIT For Example
Application 类用于保存可供您的活动使用的数据,而不是与 UI 交互。从您想要显示的活动中显示对话框。
编辑:如果您想从多个活动调用代码,您可以为这些活动创建一个超类,其中包含带有对话框的代码。然后在您想要显示对话框的所有活动中扩展这个超类,并从那里调用它。
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.