Error on dismissing ProgressDialog in AsyncTask

孤街醉人 提交于 2019-12-11 18:27:15

问题


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?


回答1:


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 :)



来源:https://stackoverflow.com/questions/9690783/error-on-dismissing-progressdialog-in-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!