0

I have a completely separate class, which extends AsyncTask, I use it for executing a html get, so it wont freeze the UI. In the constructor of the AsyncTask Im passing the activity's context, which invokes the .execute() of the task, so I can show a ProgressDialog while the htmlget is running. Here came the first problem: when rotating the phone, the current view got destroyed, and when the app reached the .dismiss() of the dialog, it crashed, since the view it was attached to does not exist anymore. I did manage to solve this issue in like 95% of the cases, however in the remaining 5%, there is still a problem. My original solution was to force disable the rotation, while the htmlget is running, by passing the activity to the AsyncTask as well, and invoking

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

in the onPreExecute(), and invoking

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

in the onPostExecute(), this way even if the user rotates the screen, it won't destroy the current activity. But, as I already mentioned, in the remaining 5%, there is still a problem, which causes force close. All I know are the following:

I have this StackTrace, which my users supplied me via Android Market:

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:381)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:226)
at android.view.Window$LocalWindowManager.removeView(Window.java:432)
at android.app.Dialog.dismissDialog(Dialog.java:278)
at android.app.Dialog.access$000(Dialog.java:71)
at android.app.Dialog$1.run(Dialog.java:111)
at android.app.Dialog.dismiss(Dialog.java:268)
at com.vasga.telvira.AsyncHttpGetter.onPostExecute(AsyncHttpGetter.java:160)
at com.vasga.telvira.AsyncHttpGetter.onPostExecute(AsyncHttpGetter.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)

it happens in the AsyncTask, on the following line(this is the 160th line mentioned in the StackTrace):

if(pd.isShowing()) pd.dismiss(); //dismiss the pd after we are done, if it is visible(it should be)

the pd.isShowing() cannot cause this, since I added that if clause as a possible solution, maybe the dialog wasn't showing on some rare occasions, but it did not work out

my application is compatible with android 1.6.x, but have a Compiling level setted for android 2.2.x in eclipse, since I have the "move to sd card" feature enabled

judging from the few user sent messages, they start the htmlget, they most likely see the ProgressDialog popping up, and then the application crashes

Any idea what can cause this? Or how to solve it? Is my NOSENSOR approach correct?

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
hundeva
  • 1,058
  • 14
  • 30
  • it's tricky. You should recreate / hide the progress dialog whenever your app pauses / resumes. – zapl Mar 13 '12 at 19:49
  • the app does not pause while this htmlget is under way, it just shouldn't, it takes 5 secs at most in a usual situation, so I dont really know why this happens. – hundeva Mar 13 '12 at 19:51
  • 1
    if screen rotates you should get `onPause`, once rotation is finished `onResume`. AsyncTask will still run. If you dynamically recreate the dialog when you resume you should have no problem. – zapl Mar 13 '12 at 19:53
  • well I did disable the rotation as far as I know, in the beginning of my AsyncTask, and reenable it at the end of the task. From what Ive learned so far, Im going to try to pass the ApplicationContext instead the Activity context, so instead of Context ctx = this; I will use Context ctx = getApplicationContext();, we shall see how it works – hundeva Mar 13 '12 at 19:57
  • won't work with applicationcontext, tho I tried some workaround.. if the error seems to be solved, I will post it as an answer, not until then, cos no point if it wont work – hundeva Mar 13 '12 at 21:51

1 Answers1

0

it seems that the error has gone, at least I did not received any new reports in the new version. What I did was, I overrided this in the main activity:

public Dialog onCreateDialog(int id)

then, I passed the activity to the task, and setted the nosensor thingie at the beginning, then when I showed the dialog, did this:

activity.showDialog(0);

then, when I had to dismiss it, I did this:

activity.dismissDialog(0);

It seems it works this way. Pardon me if there is any syntaxerror, Im not on my developer computer :)

hundeva
  • 1,058
  • 14
  • 30