I currently have an application that has many actions and should have a way to maintain state between these actions.
I use the Application class to do this, declaring my global variables and using getters and setters to interact with my actions.
I was hoping to post some custom methods there, so when I want to perform a general task, for example, display an error message, I can declare a method in my application class and call it from any activity that uses it
EscarApplication application = (EscarApplication) this.getApplication();
EscarApplication is the name of my application class above.
I 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 reuse it, but when I call it using something like below, I get an exception from the null pointer:
Visit.this.application.showError("Update error", "An error has occurred while trying to communicate with the server");
Visit the name of my current action above.
If this works, or I can only use getters and setters to change global var in the 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 the same class:
alertDialog = new AlertDialog.Builder(this).create();
and a method to call this class in the following class:
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 such activity:
EscarApplication application; application = (EscarApplication) this.getApplication(); application.showError("test", "display this message");