3

I am getting the following Exception in my code...

IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
at android.view.Window$LocalWindowManager.removeView(Window.java:432)
at android.app.Dialog.dismissDialog(Dialog.java:280)
at android.app.Dialog.access$000(Dialog.java:73)
at android.app.Dialog$1.run(Dialog.java:113)
at android.app.Dialog.dismiss(Dialog.java:270)
at com.myapp.code.SearchWord$1.handleMessage(SearchWord.java:718)  <--
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3906)
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:840)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)

I don't really understand why this error occurs. It causes force close problem on devices. Any clue on how to fix this? Thanks

This is my code...

public class SearchWord extends Activity {
/** Called when the activity is first created. */

private ProgressDialog progressDialog;
        ...
    }

    public void callDialog(){

    this.progressDialog = ProgressDialog.show(SearchWord.this,null, "Loading. Please wait..", true);

    new Thread(new Runnable(){
        public void run(){
            try{
                Thread.sleep(9000);
            }
            catch (Exception e){
                e.printStackTrace();
            }
            if(copyFlag){

            }
                            else{
                getData();
            }
            SearchWord.this.handler.sendEmptyMessage(0);
        }

    }).start();
}

    Handler handler = new Handler(){
    public void handleMessage(Message msg){
        progressDialog.dismiss();           <-- line 718
        SetData();
    }
};
wattostudios
  • 8,666
  • 13
  • 43
  • 57
dpark123
  • 65
  • 2
  • 9
  • The code you wrote is dangerous, if you leave the activity when the thread is still running, the error will occurred when the thread finished and dismiss the dialog. – dreamtale Jun 05 '12 at 14:44
  • @dreamtale can you please elaborate on that, thanks. – dpark123 Aug 03 '12 at 08:25

2 Answers2

1

I have modified your code.Look below snippet.

public void callDialog(){

            ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("Loading. Please wait..");
            progressDialog.setCancelable(false);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progressDialog.show();

            new Thread(new Runnable(){
                public void run(){
                    try{
                        Thread.sleep(9000);
                    }
                    catch (Exception e){
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if(copyFlag){
                    }else{
                        getData();
                    }
                    SearchWord.this.handler.sendEmptyMessage(0);
                }

            }).start();
        }
          // omitted

            Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg){
                progressDialog.dismiss();        
                SetData();
            }
        };
Vipul
  • 27,808
  • 7
  • 60
  • 75
  • I'm not 100% sure if this progressDialog.dismiss() indicated line error is related to that part.. Is "public void callDialog(){" causing "IllegalArgumentException: View not attached to window manager" error? Thanks. – dpark123 Jun 05 '12 at 14:21
1

I too get this error sometimes when I dismiss dialog and finish activity from onPostExecute method. I guess sometimes activity gets finished before dialog successfully dismisses.

Try below code it will help you.

try{

pd.dismiss();
// Then assign progress Dialog to null
pd = null;

}
catch(Exception e)
{
   e.printStackTrace();
}
Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36