【问题标题】:BadTokenException: Unable to add window -- token null is not valid; is your activity running? in activity.isFinishing statmentBadTokenException: 无法添加窗口——token null 无效;您的活动正在运行吗?在activity.isFinishing 语句中
【发布时间】:2017-05-31 06:12:11
【问题描述】:

如果用户仍在当前活动中,几秒钟后,我需要显示一个 popUpWindow。我实现了检查活动是否未完成/销毁然后显示弹出窗口的语句,它工作正常,对于周末用户:)(慢慢地从一个活动点击到另一个活动)但在高压测试中(活动正在重新创建、完成、快速移动形式活动到活动)这给了我这个错误:

E/UncaughtException: android.view.WindowManager$BadTokenException: 无法添加窗口——token null 无效;是你的活动 跑步? 在 android.view.ViewRootImpl.setView(ViewRootImpl.java:598) 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:341) 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) 在 android.widget.PopupWindow.invokePopup(PopupWindow.java:1279) 在 android.widget.PopupWindow.showAtLocation(PopupWindow.java:1040) 在 android.widget.PopupWindow.showAtLocation(PopupWindow.java:1003) 在 com.guides4art.app.ImageSlider.RatePopUp$3.run(RatePopUp.java:86) 在 android.os.Handler.handleCallback(Handler.java:743) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:150) 在 android.app.ActivityThread.main(ActivityThread.java:5546) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)

代码:

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

【问题讨论】:

  • 你怎么称呼showPopUpWindow
  • 我添加其余代码

标签: android android-activity popupwindow


【解决方案1】:

试试这个代码:

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

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

}, 200L);

代替:

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

还要确保将ActivityName.this 作为上下文传递.. 而不是getApplicationContext()

尝试将showPopUpWindow() 中的runnable 代码替换为:

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

【讨论】:

  • 不幸的是没有帮助:/
【解决方案2】:

代替

if(!context.isFinishing())

试试

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

对我来说工作得很好。

【讨论】:

    【解决方案3】:

    当使用弹窗时,应该有两种状态:

    1. Activity 是存活的,该Activity 没有被销毁和结束

    2. 如果创建了 Activity 或者您传递的父视图已附加到窗口 示例代码如下:

        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
        }
      

    【讨论】:

      【解决方案4】:

      希望对您有所帮助...

      替换:

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

      与:

      new Handler().postDelayed(new Runnable(){
      
          public void run() {
              pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
          }
      
      }, 100L);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-27
        • 1970-01-01
        • 2019-01-03
        • 1970-01-01
        • 2011-11-16
        相关资源
        最近更新 更多