0

The popup window has to be shown when the app is started. The function that calls the popup window is located in the OnCreate() method. When I start debugging the app it constantly crashes.

    public void ShowPopup() {

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

And this is the error message:

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
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
vitrd
  • 13
  • 6

1 Answers1

0

You need to first initialize the dialog before using it.

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();
}
Vladimír Bielený
  • 2,795
  • 2
  • 11
  • 16