android View not attached to window manager

后端 未结 13 2332
臣服心动
臣服心动 2020-11-27 10:29

I am having some of the following exceptions:

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findV         


        
13条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 10:50

    I am using a custom static class which makes- shows and hides a dialog. this class is being used by other activities too not only one activiy. Now the problem you described also appeared to me and i have stayed overnight to find a solution..

    Finally i present you the solution!

    if you want to show or dismiss a dialog and you dont know which activity initiated the dialog in order to touch it then the following code is for you..

     static class CustomDialog{
    
         public static void initDialog(){
             ...
             //init code
             ...
         }
    
          public static void showDialog(){
             ...
             //init code for show dialog
             ...
         }
    
         /****This is your Dismiss dialog code :D*******/
         public static void dismissProgressDialog(Context context) {                
                //Can't touch other View of other Activiy..
                //http://stackoverflow.com/questions/23458162/dismiss-progress-dialog-in-another-activity-android
                if ( (progressdialog != null) && progressdialog.isShowing()) {
    
                    //is it the same context from the caller ?
                    Log.w("ProgressDIalog dismiss", "the dialog is from"+progressdialog.getContext());
    
                    Class caller_context= context.getClass();
                    Activity call_Act = (Activity)context;
                    Class progress_context= progressdialog.getContext().getClass();
    
                    Boolean is_act= ( (progressdialog.getContext()) instanceof  Activity )?true:false;
                    Boolean is_ctw= ( (progressdialog.getContext()) instanceof  ContextThemeWrapper )?true:false;
    
                    if (is_ctw) {
                        ContextThemeWrapper cthw=(ContextThemeWrapper) progressdialog.getContext();
                        Boolean is_same_acivity_with_Caller= ((Activity)(cthw).getBaseContext() ==  call_Act )?true:false;
    
                        if (is_same_acivity_with_Caller){
                            progressdialog.dismiss();
                            progressdialog = null;
                        }
                        else {
                            Log.e("ProgressDIalog dismiss", "the dialog is NOT from the same context! Can't touch.."+((Activity)(cthw).getBaseContext()).getClass());
                            progressdialog = null;
                        }
                    }
    
    
                }
            } 
    
     }
    

提交回复
热议问题