我可以在我的Android应用程序类自定义全局的方法呢? [英] Can I create custom global methods in my Android Application class?
问题描述
我现在有,有许多活动,需要一个应用程序有这些活动之间保持状态的方法。
我使用Application类来做到这一点,我的声明全局变量和使用getter和setter方法与我的活动互动。
我希望把在那里一些自定义的方法,这样,当我想做一个共同的任务一样,例如,显示错误信息,我可以在我的应用程序类中声明该方法,并从任何活动叫它使用它
EscarApplication应用=(EscarApplication)this.getApplication();
EscarApplication是我上面的应用程序类的名称。
我试图包含这个方法在我的应用程序类:
公共无效showError(标题字符串,字符串消息){
Log.i(应用层,邮件);
this.alertDialog.setTitle(职称);
alertDialog.setMessage(消息);
alertDialog.setButton(OK,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
返回;
}
});
alertDialog.show();
}
在希望我还可以从活动这个方法,而不必重新声明,但是当我打电话使用类似下面我得到一个空指针异常是:
Visit.this.application.showError(更新错误,发生错误尝试与服务器通信);
访问是我的上述当前活动的名称。
这应该工作,或者我只能用getter和setter方法来改变全局变量在应用程序类。
堆栈跟踪:
了java.lang.RuntimeException:无法启动活动ComponentInfo {escar.beedge / escar.beedge.HomeScreen}:android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个应用
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
在android.app.ActivityThread.access $ 2100(ActivityThread.java:116)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1794)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:123)
在android.app.ActivityThread.main(ActivityThread.java:4203)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:521)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
在dalvik.system.NativeStart.main(本机方法)
ERROR / AndroidRuntime(375):$ android.view.WindowManager BadTokenException:产生的原因无法添加窗口 - 令牌null不是一个应用程序
在android.view.ViewRoot.setView(ViewRoot.java:460)
在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
在android.app.Dialog.show(Dialog.java:238)
在escar.beedge.EscarApplication.showError(EscarApplication.java:98)
在escar.beedge.HomeScreen.onCreate(HomeScreen.java:30)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
该对话框被声明为这样的应用程序类:
AlertDialog alertDialog;
创建于同一类:
alertDialog =新AlertDialog.Builder(本).create();
和调用它在该类中的方法如下:
公共无效showError(标题字符串,字符串消息){
alertDialog.setTitle(职称);
alertDialog.setMessage(消息);
alertDialog.setButton(OK,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
返回;
}
});
alertDialog.show();
}
最后,它是从一个活动像所谓的:
EscarApplication申请;
应用=(EscarApplication)this.getApplication();
application.showError(测试,显示此消息);
如果你需要保持活动之间的状态,然后使用服务。还有什么是黑客
I currently have an app that has many activities and needs to have a way of maintaining state between these activities.
I use the Application class to do this, declaring my global variables and using getters and setters to interact with my activities.
I was hoping to place a few custom methods in there, so that when I want to do a common task like, for instance, display an error message, I can declare the method in my application class and call it from any activity that uses it
EscarApplication application = (EscarApplication) this.getApplication();
EscarApplication being the name of my application class above.
I have tried to include this method in my application class:
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();
}
In the hope that I can call this method from activity without having to redeclare it, but when I call it using something like below I get an null pointer exception:
Visit.this.application.showError("Update error", "An error has occurred while trying to communicate with the server");
Visit being the name of my current activity above.
Should this work, or can I only use getters and setters to change global vars in an Application Class.
Stack Trace:
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)
The dialog is declared as such in the application class:
AlertDialog alertDialog;
Created in that same class:
alertDialog = new AlertDialog.Builder(this).create();
and the method to call it in that class is as follows:
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();
}
And finally, it is called from an activity like so:
EscarApplication application;
application = (EscarApplication) this.getApplication();
application.showError("test", "display this message");
If you need to maintain state between activities, then use a service. Anything else is a hack
这篇关于我可以在我的Android应用程序类自定义全局的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!