progress dialogue box giving exception in asynchtask [duplicate]
Asked Answered
G

1

5

I am developing an android application in which i m using an asynchronous task an in post execute method when i m closing progress dialogue box an exception is coming and application is closing forcefully.

Exception coming is:

    04-24 09:41:54.661: E/AndroidRuntime(1727): java.lang.IllegalArgumentException: View not attached to window manager
    04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)

The code:

data_insertion = new AsyncTask<Void, Void, Void>() {
  @Override
  protected void onPreExecute() {
    // TODO Auto-generated method stub             
    CommonUtility.show_PDialog(MainActivity.this);
    super.onPreExecute();
  }
  @Override
  protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
    //setting alaram for refresh api 
    CommonUtility.close_PDialog(); //*getting exception on this line* 
    Intent setalaram = new Intent(MainActivity.this, SetAlaram.class);
    startService(setalaram);
    Intent i = new Intent(MainActivity.this, PlayListActivity.class);
    startActivity(i);
    MainActivity.this.finish();
    super.onPostExecute(result);
    finish();
  }
  @Override
  protected Void doInBackground(Void...params) {
    // TODO Auto-generated method stub
    //some code 
  }
  return null;
}
}.execute(null, null, null); 

//and here is my close method for dilogue
public static void close_PDialog() {
  if (dialog != null && dialog.isShowing()) {
    dialog.dismiss();
  }
}

log output:

04-24 09:41:54.661: E/AndroidRuntime(1727): FATAL EXCEPTION: main
04-24 09:41:54.661: E/AndroidRuntime(1727): java.lang.IllegalArgumentException: View not attached to window manager
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.view.Window$LocalWindowManager.removeView(Window.java:432)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.app.Dialog.dismissDialog(Dialog.java:278)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.app.Dialog.access$000(Dialog.java:71)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.app.Dialog$1.run(Dialog.java:111)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.app.Dialog.dismiss(Dialog.java:268)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at commonUtilities.CommonUtility.close_PDialog(CommonUtility.java:233)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at com.walkover.filesharing.MainActivity$1.onPostExecute(MainActivity.java:55)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at com.walkover.filesharing.MainActivity$1.onPostExecute(MainActivity.java:1)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.os.AsyncTask.finish(AsyncTask.java:417)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.os.Looper.loop(Looper.java:130)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at java.lang.reflect.Method.invoke(Method.java:507)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-24 09:41:54.661: E/AndroidRuntime(1727):     at dalvik.system.NativeStart.main(Native Method)
Gonzales answered 24/4, 2013 at 9:56 Comment(8)
Post code of CommonUtility.close_PDialog(); method.Hoick
Please post logcat output?Warchaw
if you remove the line that throws the exception, is the progressDialog staying on the screen or does it disappear?Warchaw
where is "dialog" object instantiated? inside show_PDialog(MainActivity.this)?Aargau
@Warchaw I have done as you said and surprisingly exception dint come this time and also progress bar disappeared. First of all thanks a lot and can you tell me reason of this ..why this happened??Gonzales
@Ram yes inside show_PDialog(MainActivity.this) which i have been called in onPreExecute()Gonzales
@Warchaw progress dialogue bar is appearing on device screen ...it dint disappearGonzales
after doing what I wrote in my answer?Warchaw
W
1

Possibly when the task ends, and runs on its onPostExecute, the Activity it was created in (and it got the context from) was already destroyed when it came to the onPostExecute.

You could leave it this way, or make one progressDialog instance somewhere that can be used every time you need a dialog, and in the onDestroy() method of your activity you could cancel it (in case something like this happens).

Warchaw answered 24/4, 2013 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.