PopupWindow $BadTokenException: Unable to add window — token null is not valid

后端 未结 11 2100

I have the following error when showing a PopupWindow. The errors are triggered by the line:

checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gra         


        
相关标签:
11条回答
  • 2020-11-27 05:16

    There are two scenarios when this exception could occur. One is mentioned by nandeesh. Other scenario is mentioned here: http://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/

    Make sure you handle both of them

    0 讨论(0)
  • 2020-11-27 05:17

    Try to use it

    LayoutInflater inflater = (LayoutInflater).getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflate.from(YourActivity.this).inflate(R.layout.yourLayout, null);
    
    0 讨论(0)
  • 2020-11-27 05:18

    Following many hours of search and testing i found following solution(by implementing different SO solutions) here it what didn't failed in any case i was getting crash.

         Runnable runnable = new Runnable() {
                @Override
                public void run() {
    
              //displayPopup,progress dialog or what ever action. example
    
                    ProgressDialogBox.setProgressBar(Constants.LOADING,youractivityName.this);
                }};
    

    Where logcat is indicating the crash is happening.. start a runnable .in my case at receiving broadcast.

    runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(!isFinishing()) {
                        new Handler().postAtTime(runnable,2000);
                    }
                }
            });
    
    0 讨论(0)
  • 2020-11-27 05:22

    Try to show the pop like below

    findViewById(R.id.main_layout).post(new Runnable() {
            public void run() {
                mPopupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);
                Button close = (Button) customView.findViewById(R.id.btn_ok);
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        doOtherStuff();
                    }
                });
            }
        });
    
    0 讨论(0)
  • 2020-11-27 05:23
      @Override
    protected void onCreate(Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.popup_window_layout, new LinearLayout(mContext), true);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(view);
    }
    
       @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus) {
            popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
        }
    }
    

    the correct way is popupwindow.show() at onWindowFocusChanged().

    0 讨论(0)
  • 2020-11-27 05:25

    Use:

    YourActivityName.this
    

    Instead of:

    getApplicationContext();
    
    0 讨论(0)
提交回复
热议问题