简体   繁体   中英

Android popup on activity create

I am using the following code to create a popup which shows as the activity loads. I call loadingPopup(); in my oncreate method. The popup works the problem is when ever i try to place onclick listener for the dismiss button the whole thing crashes on load. If i were to remove the DissMissButton onclick listener code it works fine. Any help would be appreciated

 private void loadingPopup() {
       LayoutInflater inflater = this.getLayoutInflater();
       final View layout = inflater.inflate(R.layout.popup_welcome, null);
       final Button DismissButton = (Button) findViewById(R.id.button_welcomepopup_dismiss);
       final PopupWindow Welcome_popupWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

       Welcome_popupWindow.setFocusable(false);
       Welcome_popupWindow.setTouchable(true);
       Welcome_popupWindow.setOutsideTouchable(true);

       layout.post(new Runnable() {

           public void run() {
               Welcome_popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
               DismissButton.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       Welcome_popupWindow.dismiss();
                   }
               });
           }

       });

You call find view on your activity view, while your button is in the popup, so of course the DismissButton is null and app force close.
Change the find view code to this:

       final Button DismissButton = (Button) layout.findViewById(R.id.button_welcomepopup_dismiss);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM