23683

Error on dismissing ProgressDialog in AsyncTask

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?

Answer1:

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

Recommend

  • How to stay in an activity if the Internet connection is lost in between an asynchorous task
  • Android : How can I pause the main thread till AsyncTask completes?
  • Android ListView NullPointerException
  • skia decoder->decode returned false when download image and display in imageview
  • AsyncTask and Thread won't run twice in java / android application
  • Trying to fetch JSON with Android using Unirest
  • How to Call AsyncTask from Adapter class?
  • OnPostExecute Method is Executing before Doinbackground Fetching Data from Server To Local
  • How to load replace json values in spinner?
  • Start activity in doInBackground method
  • How to solve mysqli_fetch_array() expects parameter 1 to be mysqli_result,string in php
  • How to return the id of selected item of listview
  • Android asynctask warring like arargs methods should only override or be overridden by other varargs
  • What change did really happen in Async Task after Android Gingerbread?
  • Android - How to use ratingbar in listview to send data into mysql?
  • Android progress dialog for Listview
  • Android: Progress Bar to upload Object into server
  • Android Data Fetch From Database Not Working (php + json + mysql)
  • How to pass My JSON array as a parameter in URL
  • how to start the same asynctask on again clicking on the button [duplicate]
  • Android: Show progress indicator of parse while downloading a XML
  • Android Class with sub-class AsyncTask wait for the postExecute before returning the value
  • IllegalStateException in AsyncTask
  • Android Service Stops Running After Awhile Please Help
  • Android Paypal refund through App
  • Going from Listactivity to Listfragment
  • asynctask cancel onpostexecute
  • Gridview getChildAt hidden fields
  • Error on dismissing ProgressDialog in AsyncTask
  • Acess Nested JSON Android
  • Android language parsing difficulties
  • Getting data from Parse.com using fragments
  • Google Map Undesirably Resets camera zoom out
  • Android Generic AsyncTask Class
  • How to reduce SSRS PDF Export Size
  • Android image didn't display to ImageView from image URL
  • Java AsyncTask passing variable to main thread
  • I can't get setInterval() to work in Javascript
  • Quick Question About Get and Set
  • How to upload specific List image using click on Upload button