【发布时间】:2010-08-17 15:04:53
【问题描述】:
我目前有一个包含许多活动的应用程序,并且需要有一种方法来维护这些活动之间的状态。
我使用 Application 类来执行此操作,声明我的全局变量并使用 getter 和 setter 与我的活动进行交互。
我希望在其中放置一些自定义方法,这样当我想做一个常见的任务时,例如显示一条错误消息,我可以在我的应用程序类中声明该方法并从任何活动中调用它使用它
EscarApplication application = (EscarApplication) this.getApplication();
EscarApplication 是我上面的应用程序类的名称。
我试图在我的应用程序类中包含这个方法:
public void showError(String title, String message) {
Log.i("Application level",message);
this.alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
return;
}
});
alertDialog.show();
}
希望我可以从活动中调用此方法而不必重新声明它,但是当我使用如下所示的方法调用它时,我得到一个空指针异常:
Visit.this.application.showError("Update error", "An error has occurred while trying to communicate with the server");
Visit 是上面我当前活动的名称。
这是否可行,或者我只能使用 getter 和 setter 来更改应用程序类中的全局变量。
堆栈跟踪:
java.lang.RuntimeException: Unable to start activity ComponentInfo{escar.beedge/escar.beedge.HomeScreen}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$2100(ActivityThread.java:116)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4203)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(375): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:460)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:238)
at escar.beedge.EscarApplication.showError(EscarApplication.java:98)
at escar.beedge.HomeScreen.onCreate(HomeScreen.java:30)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
对话框在应用程序类中是这样声明的:
AlertDialog alertDialog;
在同一个类中创建:
alertDialog = new AlertDialog.Builder(this).create();
在该类中调用它的方法如下:
public void showError(String title, String message) {
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
return;
}
});
alertDialog.show();
}
最后,它是从这样的活动中调用的:
EscarApplication application;
application = (EscarApplication) this.getApplication();
application.showError("test", "display this message");
【问题讨论】:
-
这是重复的,这里已经详细解答了:stackoverflow.com/questions/708012/…
-
这不是一个重复的问题。我一直在使用应用程序类来访问全局变量。我问的问题是我可以有全局方法吗,特别是我可以有一个可以从任何活动触发的全局 AlertDialog
-
我不确定你的意思。我在最初的问题中已经说过,我有一个用于记录状态的应用程序类。那就是你链接所指的。我想知道我是否可以超越仅仅设置和获取全局变量,以及我是否可以拥有可以从任何活动中调用的全局方法,例如我想要设置的自定义 AlertDialog。如果你的意思是我是否尝试过这个,然后是的,我有,正如我原来的问题中所述,当我尝试调用我的警报对话框时,我得到一个空点异常错误。
-
您可以为您的 NPE 发布堆栈跟踪吗?我感觉你的 alertDialog 在调用 showError 之前没有被初始化。
-
嗨,Erich,我在问题中包含了一个堆栈跟踪,以及我如何尝试调用它的详细信息。 - 谢谢