View not attached to window manager, dialog dismiss

后端 未结 5 847
北海茫月
北海茫月 2020-12-29 09:50

So I\'ve activity called GameActivity.java and in this activity I call DialogAnswer.show() which simple shows some picture on screen.



        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 10:22

    using try catch may not be a efficient way to solve this problem as may cause memory leak; for this question, as the context is used as param, so before using the code dialog.dismiss, we can use codes below to protect:

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

    also, another method can be used to patch this crash in the function of onDestroy() in the activity, add the code:

    protected void onDestroy() {
      if(dialog != null){
         dialog.dismiss();
         dialog = null;
      }
    }
    

提交回复
热议问题