我希望在特定条件后显示一个对话框,但是对于现在的演示,我想要从扩展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
任何人都可以建议任何解决方案吗?
答案 0 :(得分:7)
**请记住,您需要考虑其行为的后果。
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());
}
}
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:
您不能**通过不是活动或服务的上下文显示应用程序窗口/对话框。尝试传递有效的活动参考
**您可以使用应用程序上下文通过在调用Dialog.show()之前添加来创建对话框;
Dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
- but this requires permission:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
参考:
答案 1 :(得分:2)
您无法使用应用程序[或服务]上下文。如果您确实希望从应用程序向您显示拨号日志,则必须向其传递活动上下文。您也可以存储活动上下文,但我执行不 推荐。活动背景在结束时无效,因此您将破坏您的计划。正如@LeoLink所说,只需直接从你的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();
}
}
答案 2 :(得分:1)
Application类用于保存您的活动可以使用的数据,而不是与UI交互。从您希望它显示的活动中显示对话框。
编辑:如果要从多个活动中调用代码,可以为这些活动创建一个超类,其中包含对话框中的代码。然后在要显示对话框的所有活动中扩展此超类,并从那里调用它。