Within a public class that doesn’t extend any other class I’m doing some evaluation stuff and I would like to return a Dialog if something needs to be communicated to the user.
I’m using an AsyncTask to run a method of an instance of this class from the main UI:
private OnlineActivities onlineActivities = new OnlineActivities();
new DoOnlineActivity().execute(getApplicationContext());
private class DoOnlineStuff extends AsyncTask<Context, Integer, Dialog> {
@Override
protected Dialog doInBackground(Context... params) {
return onlineActivities.start(params[0]);
}
@Override
protected void onProgressUpdate(Integer... progress) {
Log.v(RuntimeVars.getMyName(), "AsyncOnlineTask in progess");
}
@Override
protected void onPostExecute(Dialog result) {
if (result != null)
result.show();
}
}
Now, the public Dialog start() method of OnlineActivities.java creates a dialog using the Context that was assigned via it’s parameter.
When the Dialog is returned to onPostExecute I receive the following exception
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): FATAL EXCEPTION: main
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at android.view.ViewRoot.setView(ViewRoot.java:509)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at android.app.Dialog.show(Dialog.java:241)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at info.myProject.messaging.ConversationList$DoOnlineActivity.onPostExecute(ConversationList.java:245)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at info.myProject.messaging.ConversationList$DoOnlineActivity.onPostExecute(ConversationList.java:1)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at android.os.AsyncTask.finish(AsyncTask.java:417)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at android.os.Looper.loop(Looper.java:123)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at java.lang.reflect.Method.invoke(Method.java:521)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): at dalvik.system.NativeStart.main(Native Method)
I suppose I cannot use the context like this. But what would be the alternative? How can I outsource some basic stuff I always need to do, even some general Dialogs?
Do I need to use custom intents instead?
I got a part of it to work. The problem was the usage of
getApplicationContext(). The Android SDK statesI suppose this context differs from the context of the actual activity.
So, the Dialog gets displayed when I use
thisinstead ofgetApplicationContext():BUT: Now the button I created within the Dialog won’t do anything. I guess the
OnClickListener()within my Dialog isn’t responsive in the context of my main ui:So the last step should be to tell the
OnClickListener()which context to use. But how can this be done?new Dialog.OnClickListener()won’t take any parameter about the context?Thanks!
S.