2012-06-05 82 views
3

我得到在我的代碼如下例外...的Android java.lang.IllegalArgumentException異常:查看未連接到窗口管理器

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) 

我真的不明白,爲什麼這個錯誤發生。它會導致設備上的強制關閉問題。有關如何解決此問題的任何線索?由於

這是我的代碼...

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(); 
    } 
}; 
+0

您編寫的代碼是危險的,如果在線程仍在運行時離開活動,則線程完成並關閉對話框時將發生錯誤。 – dreamtale

+0

@dreamtale請你詳細說明一下,謝謝。 – dpark123

回答

1

我已經修改了以下片斷您code.Look。

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(); 
      } 
     }; 
+0

我不是100%確定這個progressDialog.dismiss()指出的行錯誤是否與該部分有關。「public void callDialog(){」 導致「IllegalArgumentException:視圖不附加到窗口管理器」錯誤?謝謝。 – dpark123

1

我也有時得到這個錯誤有時當我關閉對話框並完成onPostExecute方法的活動。我猜有時活動會在對話成功解散之前完成。

嘗試下面的代碼,它會幫助你。

try{ 

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

} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
相關問題