Я хочу / пытаюсь показать всплывающее окно при первом запуске приложения, но получил эту ошибку
java.lang.NullPointerException: попытка вызвать виртуальный метод void android.widget.PopupWindow.showAtLocation (android.view.View, int, int, int) 'для ссылки на пустой объект
Я выполняю наложение, чтобы запустить приложение с всплывающим окном при первом запуске.
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.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)
Любое предложение для решения моего кода или лучший способ сделать Всплывающее окно