2013-10-23 39 views
11

所以我叫活動和GameActivity.java這個活動我打電話DialogAnswer.show()這簡單的在屏幕上顯示的一些圖片。查看未連接到窗口管理器,對話框駁回

java.lang.IllegalArgumentException: View not attached to window manager 
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:402) 
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:304) 
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79) 
at android.app.Dialog.dismissDialog(Dialog.java:325) 
at android.app.Dialog.dismiss(Dialog.java:307) 
at pl.evelanblog.prawdaczyfalsz.screen.DialogAnswer$1.onFinish(DialogAnswer.java:36) 
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5328) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
at dalvik.system.NativeStart.main(Native Method) 

這是我DialogAnswer.java

public class DialogAnswer extends Activity { 

    private static ImageView resultImage; 
    private static Dialog dialog = null; 

    public static void show(Context context, boolean fCorrect) { 

     dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen); 
     dialog.setContentView(R.layout.dialog); 
     resultImage = (ImageView) dialog.findViewById(R.id.result_image); 

     if (fCorrect) 
      resultImage.setImageResource(R.drawable.correct_image); 
     else 
      resultImage.setImageResource(R.drawable.incorrect_image); 

     dialog.show(); 

     new CountDownTimer(700, 100) { 
      public void onTick(long millisUntilFinished) { 
      } 
      public void onFinish() { 
       dialog.dismiss(); //this is line 36 
      } 
     }.start(); 
     } 
} 

GameActivity.java有時我會喜歡這個活動的另一個即時得到錯誤在我的崗位上。我不知道如何解決這個問題,它很難調試,因爲它很少有錯誤,但它存在。

+0

這個問題是一個傻瓜,答案和幼稚,所以幫助別人就這個問題誰絆倒,解決方案的最好的總結,我發現到目前爲止這裏是:https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/ –

+0

你可以參考Ref ference從[這裏](https://stackoverflow.com/questions/2745061/java-lang-illegalargumentexception-view-not-attached-to-window-manager/45346389#45346389) –

回答

5

使用try語句。

new CountDownTimer(700, 100) { 
     public void onTick(long millisUntilFinished) { 
     } 
     public void onFinish() { 
      try { 
       dialog.dismiss(); 
       dialog = null; 
      } catch (Exception e) { 
       //TODO: Fill in exception 
      } 
     } 
    }.start(); 
+9

不知道這怎麼可以幫助到,你只是修補問題(根本沒有優化,因爲處理異常很慢),而不是提供真正的解決方案。 – Aballano

+0

我猜測它是通過直接解決OP的問題/錯誤而得到幫助的,您是否對這個問題有「真正的」解決方案? – frogmanx

+1

@ kalyan-pvs提供的那個爲我工作。 – Aballano

2

做到這一點的方式

new CountDownTimer(700, 100) { 
      public void onTick(long millisUntilFinished) { 
      } 
      public void onFinish() { 
       runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         dialog.dismiss(); //this is line 36 

        } 
       }); 
      } 
     }.start(); 
+0

這個工程...? –

18

onDestroy()onStop() method..You駁回檢查像在此之前是簡單解聘不檢查它是否被顯示或不

if (mDialog!=null) { 
    if (mDialog.isShowing()) { 
     mDialog.dismiss();  
    } 
} 
+3

如果將兩個語句放在一個條件中,是否有區別? 'if(null!= pDialog && pDialog.isShowing()){pDialog.dismiss(); }' - >這引發'拋出:IllegalArgumentException:查看未連接到窗口manager' –

+0

裏面'我的AsyncTask的onPostExecute',我不知道爲什麼它平息引發的異常。 –

+0

不要忘記'超級。onDestroy'或'super.onPause' –

25

很多人可能會使用Google這個,所以我可能把我的2P在:

不幸的是,那裏的人都使用isShowing()不會因爲這個工作仍然可以返回true時的例子視圖被分離(活動已經結束)。

如果你是懶惰的,其他海報評論關於將其封裝在try {}中也可以在/ most /情況下工作(儘管有一些情況下系統可能會關閉它並且異常仍然會導致強制關閉,你不能把一個try {}一輪談判,在Android的代碼發生了,不是你)

最好的解決方法是,當你的活動結束/關閉關閉對話框。如果你試圖關閉用戶導航離開,而你的任務異步運行(或者,電話鈴響了,它的導航離開他們)就後,那麼你會得到ViewNotAttached例外。

+3

「,儘管有幾種情況下系統可能會關閉它,並且異常仍然會導致強制關閉,您不能嘗試在android代碼中發生嘗試,而不是您的」示例? – xmen

+2

有史以來最好的答案! –

1

使用嘗試捕捉可能不是解決這個問題,因爲可能會導致內存泄漏一個有效的方法; 對於這個問題,根據上下文用作PARAM,所以使用的代碼dialog.dismiss之前,我們可以使用下面的代碼來保護:

public void onFinish() { 
    if{ctx instanceof Activity && !((Activity)ctx.isfinishing()){ 
      dialog.dismiss(); //this is line 36 
    } 
} 

還,另一種方法可以用於修補此崩潰 的onDestroy()在活動的功能,添加代碼:

protected void onDestroy() { 
    if(dialog != null){ 
    dialog.dismiss(); 
    dialog = null; 
    } 
} 
相關問題