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

后端 未结 11 2178

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: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().

提交回复
热议问题