8

I have a popUpWindow that need to by displayed, after couple of seconds if the user is still on current activity. I implemented stament that check if the activity isnt finished/destroyed and then display the Popup, and it works fine, for weekend users :) ( slowly clicking from activity to activity) but in high pressuretests ( activities are recreating, finished, fast move form activity to activity) that gives me that error :

E/UncaughtException: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:598) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:341) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) at android.widget.PopupWindow.invokePopup(PopupWindow.java:1279) at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1040) at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1003) at com.guides4art.app.ImageSlider.RatePopUp$3.run(RatePopUp.java:86) at android.os.Handler.handleCallback(Handler.java:743) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:150) at android.app.ActivityThread.main(ActivityThread.java:5546) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)

code:

   private void showPopUpWindow(final Activity context){


       popupWindow = new PopupWindow(context);

       LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(
               LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
       popupWindow.setHeight(layoutParams.height);
       popupWindow.setWidth(layoutParams.width);
       popupWindow.setOutsideTouchable(true);
       popupWindow.setTouchable(true);
       popupWindow.setFocusable(true);
       popupWindow.setContentView(view);


       ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
           @Override
           public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
               if(context instanceof CarSale) {
                   ((CarSale) context).saveRate((int) rating);
                    ((CarSale) context).initRate();
                   title.setText(""+context.getString(R.string.thanksForRate));
               }
               else
                   Log.i("kamil","error");
           }
       });
       closeButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               popupWindow.dismiss();
           }
       });


if(!context.isFinishing() || !context.isDestroyed() )
               activityView.post(new Runnable() {
                   @Override
                   public void run() {
                       popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
                   }
               });
   }


//View Pager Class

 @Override
    public void onPageSelected(int position) {
        if(viewPager !=null){
        this.position=position;
        if(position==carList.size()-1 && isRated() && showRateBar)
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                 new RatePopUp(Cars.this,activityView);
                    showRateBar=false;
                }
            },5*SECOND);

//RatePopUp constructor

 public RatePopUp(Activity context,View activityView){
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.rate_popup_layout, null);
        this.activityView=activityView;
        ratingBar = (RatingBar) view.findViewById(R.id.ratingPop);
        title= (TextView)view.findViewById(R.id.rateTitle);
        title.setText(context.getString(R.string.rate_exhibition));
        closeButton = (Button)view.findViewById(R.id.close_button);
        Typeface  typeface =Typeface.createFromAsset(context.getAssets(),"fonts/fontawesome-webfont.ttf");
        closeButton.setTypeface(typeface);
        closeButton.setText(context.getString(R.string.exitIcon));
        showPopUpWindow(context);
    }
Expiredmind
  • 788
  • 1
  • 8
  • 29

4 Answers4

4

try this code:

 new Handler().postDelayed(new Runnable(){

    public void run() {
       popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
    }

}, 200L);

instead of:

popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);

Also make sure you pass ActivityName.this as context.. not getApplicationContext()

Try replace your runnable code in showPopUpWindow() with this:

runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (!isFinishing()) {
                popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
            }
        }
    });
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
0

Instead of

if(!context.isFinishing())

try

if(!((Activity) context).isFinishing())

Worked fine for me.

Dmitriy
  • 5,525
  • 12
  • 25
  • 38
0

When using popup window you should make should two states:

  1. Activity is alive that the activity is not destroyed and finished

  2. make should the activity is created or the parent view you pass has attached to window The example code as followed:

      val decorView = ctx.window.decorView
      decorView.post {
           val posArr = IntArray(2)
           anchorView.getLocationInWindow(posArr)
           val yOffset = posArr[1] - anchorView.height + verticalOffset
           if (isActivityAlive(ctx) && decorView.isAttachedToWindow) {
               pop.showAtLocation(decorView, Gravity.TOP, 0, yOffset)
           }
      }
    
      private fun isActivityAlive(ctx: Activity?): Boolean {
           return ctx != null && !ctx.isDestroyed && !ctx.isFinishing
      }
    
qianlv
  • 502
  • 5
  • 15
-2

Hope this will help you...

Replace:

pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);

with:

new Handler().postDelayed(new Runnable(){

    public void run() {
        pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
    }

}, 100L);
Mahendran Candy
  • 1,114
  • 17
  • 17