调用弹出 window 时应用程序崩溃

App crashes when the popup window is called

应用程序启动时必须显示弹出窗口 window。调用弹出窗口的函数 window 位于 OnCreate() 方法中。当我开始调试应用程序时,它经常崩溃。

    public void ShowPopup() {

       dialog.setContentView(R.layout.mainactiv);
       dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
       dialog.show();
    }

这是错误信息:

java.lang.RuntimeException: Unable to start activity ComponentInfo

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Dialog.setContentView(int)' on a null object reference

使用前需要先初始化对话框

public void ShowPopup() {
    dialog = new Dialog(this);    // Initialize dialog before use
    dialog.setContentView(R.layout.mainactiv);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();
}