-2

I want/try to show Popup Window on first launch app but got this error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.PopupWindow.showAtLocation(android.view.View, int, int, int)' on a null object reference

I make overlay activity to start the app with Popup on first launch.

MainActivity.java

public class MainActivity extends AppCompatActivity implements NetworkStateReceiver.NetworkStateReceiverListener {

    //PopUp
    Context popupContext;
    Activity popupActivity;
    PopupWindow popupWindow;
    RelativeLayout popupLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        popupWindow.showAtLocation(popupLayout, Gravity.CENTER, 0, 0);

        (***)

        //Start PopUp
        popupContext = getApplicationContext();
        popupActivity =  MainActivity.this;
        LayoutInflater inflater = (LayoutInflater) popupContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.activity_start, null);
        popupWindow = new PopupWindow(popupView,
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        if (Build.VERSION.SDK_INT >= 21){
            popupWindow.setElevation(5.0f);
        }
    }
}

Error Message

05-08 09:48:18.612 7398-7398/id.nax.androidstate E/AndroidRuntime: FATAL EXCEPTION: main
    Process: id.nax.androidstate, PID: 7398
    java.lang.RuntimeException: Unable to start activity ComponentInfo{id.nax.androidstate/id.nax.androidstate.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.PopupWindow.showAtLocation(android.view.View, int, int, int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
        at android.app.ActivityThread.access$1100(ActivityThread.java:229)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7406)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.PopupWindow.showAtLocation(android.view.View, int, int, int)' on a null object reference
        at id.nax.androidstate.MainActivity.onCreate(MainActivity.java:53)
        at android.app.Activity.performCreate(Activity.java:6904)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:7406) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Any Suggestion to solve my code or better method to make Popup Window?

Devil10
  • 1,853
  • 1
  • 18
  • 22
Naxsami
  • 123
  • 4
  • 16
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM May 08 '19 at 03:58
  • You forgot to create `PopUpWindow` instance. And Do not use `getApplicationContext()` Use `MainActivity.this` to create `PopUpWindow`. – ADM May 08 '19 at 04:00

2 Answers2

1

You're trying to call showAtLocation(...) before you actually create the PopupWindow.

    popupWindow.showAtLocation(popupLayout, Gravity.CENTER, 0, 0);

    (***)

    //Start PopUp
    popupContext = getApplicationContext();
    popupActivity =  MainActivity.this;
    LayoutInflater inflater = (LayoutInflater) popupContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.activity_start, null);
    popupWindow = new PopupWindow(popupView,
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    if (Build.VERSION.SDK_INT >= 21){
        popupWindow.setElevation(5.0f);
    }

You can do it like this instead, I also refactored the code to be a little bit cleaner for you.

    // The View you're making the popup in regards to.
    View parent = findViewById(R.id....);

    View popupView = LayoutInflater.from(this).inflate(R.layout.activity_start, null);
    popupWindow = new PopupWindow(popupView,
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);

    if (Build.VERSION.SDK_INT >= 21){
        popupWindow.setElevation(5.0f);
    }
    popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
advice
  • 5,778
  • 10
  • 33
  • 60
  • now i got new error massage `java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.IBinder android.view.View.getWindowToken()' on a null object reference` – Naxsami May 08 '19 at 03:51
  • @ForceGaming What line? – advice May 08 '19 at 04:15
  • --------> `popupWindow.showAtLocation(popupLayout, Gravity.CENTER, 0, 0);` <----- this line – Naxsami May 08 '19 at 05:14
  • @ForceGaming Okay, I believe the issue is that you were passing `popupLayout`. You need to pass a `View` that you're making the popup a child of. For example, you may pass a `Button` that you're making the popup near. – advice May 08 '19 at 05:27
0

You call this method on a null object you need to call it after you create the popupwindow and not before. Put the the call after the line: popupWindow = new PopupWindow(popupView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

Raz
  • 8,918
  • 3
  • 27
  • 40