从oncreate调用android弹出窗口

abe*_*bed 3 android popupwindow

private void loadingPopup() {
    LayoutInflater inflater = this.getLayoutInflater();
          View layout = inflater.inflate(R.layout.loading_dialog, null);

        PopupWindow windows = new PopupWindow(layout , 300,300,true);
       windows.setFocusable(false);
          windows.setTouchable(true); 
          windows.setOutsideTouchable(true);
          windows.showAtLocation(layout,Gravity.CENTER, 0, 0);

}
Run Code Online (Sandbox Code Playgroud)

loadingPopup()oncreate()累积的异常中调用方法时......请帮助我

Sou*_*rma 10

您甚至在显示活动窗口之前尝试显示弹出窗口.在post方法的帮助下,我们可以等到所有必要的启动生命周期方法完成.

试试这个 :

private void loadingPopup() {
    LayoutInflater inflater = this.getLayoutInflater();
    final View layout = inflater.inflate(R.layout.loading_dialog, null);

    final PopupWindow windows = new PopupWindow(layout , 300,300,true);
    windows.setFocusable(false);
    windows.setTouchable(true); 
    windows.setOutsideTouchable(true);
    layout.post(new Runnable() {
        public void run() {
            windows.showAtLocation(layout,Gravity.CENTER, 0, 0);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)