java.lang.NullPointerException: 尝试在空对象引用上调用虚方法“”

java.lang.NullPointerException: Attempt to invoke virtual method '' on a null object reference

我 want/try 在首次启动应用程序时显示弹出窗口 Window 但出现此错误

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

我制作覆盖 activity 以在首次启动时使用弹出窗口启动应用程序。

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);
        }
    }
}

错误信息

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

任何解决我的代码的建议或制作弹出窗口的更好方法Window?

您在需要在创建弹出窗口之后而不是之前调用它的空对象上调用此方法。 将电话放在该行之后: popupWindow = new PopupWindow(popupView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

您试图在实际创建 PopupWindow 之前调用 showAtLocation(...)

    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);
    }

你也可以这样做,我还重构了代码,让你更干净一些。

    // 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);